How to pass function input as table name?

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.

回答(1 个)

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 个评论

Thanks for that reference. However, it still won't let me use a string (determined by the function) as the name of the table. So, if I have (in a function):
output = myfunction(string)
rtable = [string '_r']
ltable = [string '_r']
I can't use the new table names in a function:
ltable(rows,vars) = ...
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.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Tables 的更多信息

产品

提问:

2015-8-12

评论:

dpb
2015-8-12

Community Treasure Hunt

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

Start Hunting!

Translated by