How do I: Create a table using a Query on a Larger Table

8 views (last 30 days)
Hello,
I have a "master" table of data with "x" number of rows and "y" number of columns. The table contains test results for "x" number of specimens. There are "y" number of measured results. However, not every possible measured value was recorded for each "x" test. This is a compilation of test data from various researchers, so there are naturally gaps in the data.
What I am currently doing is querying the master table to perform calculations on the data. Of course I can't perform calculations on the entire table at once, so I have to select which rows I want to use as inputs to my calculation. So I have something like this:
T = allData;
rows = T.Form=="Round" & T.Color=="Blue" & T.Author=="Smith";
vars ={'Form','Color','Author','Mass','Height','Speed');
T_calc = T(rows,vars);
I select the rows and the vars manually based on the calculation that I want to do, and then do those calculations using the T_calc table.
This can be time consuming, but the real issue is that if I want to do the same calculation using data from two authors:
rows = T.Author=="Smith" & T.Author=="Jones"
I can't find a way to do it. The code above is just an example to give you an idea of what I am trying to do, but I haven't found a way to do it. So my questions are:
  • Is there a better way to do this (It'd be nice to simply have a form that I can select what I want for "rows" and "vars" and hit go.
  • If the way I am doing it now is the easiest way, how can I do: T.Author==EitherOr?
I imagine there is a much easier way but I haven't found it just yet.
Thank you so much!

Answers (1)

Peter Perkins
Peter Perkins on 8 Mar 2018
A couple things:
  • I think the short answer is that you just want a | (vertical bar) rather than a &, right? You could also use ismember, but for two authors, probably just a good to spell it out.
  • If you haven't already done so, it's likely that you want to convert some of your data from text to categorical.
  • If you find yourself needing to do calculations across all groups in your data, you may find varfun or rowfun, with the GroupingVariables parameter, useful. Hard to say without know what you're doing.
Hope this helps.
  1 Comment
David
David on 9 Mar 2018
Hello Peter,
The columns associated with the rows that I am querying (Author, Form, Color,etc) are all categorical, as you've recommended. I don't actually know the significance of the Categorical classification; however.
It does seem like this works:
rows = T.Author=="Smith" | T.Author=="Jones"
but If I structure it like this:
rows = T.Author=="Smith" | T.Author=="Jones" &...
T.Color=="Blue";
Then I do get a table for that gives me Smith and Jones, but I get every color associted with Smith AND Jones, not JUST blue.
I do very much like the findgroups function, perhaps I should try using that instead.
Is there any way to update the query to return both authors and only the color Blue? That would be a short term fix for me. Long term I should perhaps use more logical groups.
Thanks again!

Sign in to comment.

Categories

Find more on Tables in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!