How to pass function input as table name?
Afficher commentaires plus anciens
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.
Réponses (1)
dpb
le 12 Août 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 commentaires
Ben Taylor
le 12 Août 2015
Modifié(e) : Ben Taylor
le 12 Août 2015
dpb
le 12 Août 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.
Catégories
En savoir plus sur Tables dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!