0

Can I do SQL-style query on an in-memory dataset (or cellarray, or structure, etc) in MATLAB?

Why I ask is, sometimes, I don't want to talk to the database for 1000 times when I want to do different operations on each of the 1000 rows of data. Instead, I'd rather read all 1000 from the database and operate on them in MATLAB.

For example, I have read the following out of from the database:

age  first_name  last_name     income

30   Mike        Smith         45

17   David       Oxgon         17

22   Osama       Lumbermaster  3

Now I want to find out the full names of the people that are under the age of 25. I know how to do it, but is there any syntax as clean and intuitive as SQL like this?

SELECT first_name + ' ' + last_name AS name FROM people WHERE age < income
3
  • please describe the specific query you're after Commented Jun 18, 2015 at 19:18
  • Is that a cell array or a table ? Commented Jun 18, 2015 at 22:31
  • In this case, a table. I'm looking for if there is some kind of clean syntax in general, like SQL or LINQ. I mean, in MATLAB different things take very different forms. For example "equal" is "=" for numeric but have to use is strcomp(). And there is also the difference between mat and cell, which makes statements that are essentially of the same logic take various forms. It not only makes it harder to read and think, but also prevent you from making the code more generic and extendable. Commented Jun 18, 2015 at 22:49

1 Answer 1

1

In the docs page Access Data in a Table (see the example Index Using a Logical Expression) it shows that your examples could be achieved as follows:

MyTable({'first_name','last_name'}, MyTable.age < MyTable.income)

These docs don't specifically explain how to merge the name and surname into one variable but I'm sure it's easy. Give it a try and let us know if you get it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Dan! Will for sure try it out when and come back to this when I have MATLAB on my hands again:)
I was hoping not needing to specify the table name again as it's already apprent. Ideally it would be like this: MyTable({'first_name','last_name'}, age < income). This way, if the table name is long or if there are multiple filtering criteria, the syntax can still be concise. However, seems this does not exist outside of SQL (maybe LINQ.Net), not even in python pandas. I guess this is as good as we get. Accepting your answer, Dan, apologies for the super long delay.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.