How to pass function input as table name?
Show older comments
I am trying to import experimental results into MATLAB as tables. I have a function written to import the data from spreadsheets for each trial. I want to aggregate one column from each of the imported tables into a new table. I want to have a function importKinematics(coord) where I input a string (the name of a coordinate to track) and it creates the table. I currently have this code in a script:
TARGET = table;
TARGET.gait_cycle = (0:0.1:100)';
TARGET.NLTrial01 = interp1(NLcmcKinematicsTrial01.gait_cycle, ...
NLcmcKinematicsTrial01.TARGET_l, ...
TARGET.gait_cycle,'spline');
For each coordinate, I do a 'find and replace' to replace TARGET with the coordinate name. I can't figure out how to take the input of a function and pass it as a table name.
Answers (1)
dpb
on 12 Aug 2015
Use a cell string (see the example at "Index Using a Logical Expression" which shows both a logical and a variable reference while the example at the preceding of "by Name" only illustrates a hardcoded text value--a weak point in the doc that should be addressed...here's the relevant code snippet reproduced, though--
rows = patients.Age<30;
vars = {'Gender','Height','Weight'};
T3 = patients(rows,vars)
Note the use of vars in the second dimension reference containing the cellstring variables.
2 Comments
Ben Taylor
on 12 Aug 2015
Edited: Ben Taylor
on 12 Aug 2015
dpb
on 12 Aug 2015
OK, sorry, I missed that it's actually the table itself you're trying to dynamically name.
That's a frowned-upon construction in Matlab to "poof" variables into the workspace; it's doable but not recommended as it then requires eval to execute code using the new name. The usual recommendation for workaround is dynamic field names in a general structure or the like; or, in this case it would seem perhaps you could manage with a "table of tables" or maybe a structure array of tables; I'm not sure as I don't have a release which includes the table datatype with which to 'spearmint, sorry.
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!