How to extract variable data from a table
3 views (last 30 days)
Show older comments
I have some code for data analysis that I am trying to make more general by including a for loop and then extracting data from a table based on input from a text file, but am struggling to extract the data for some reason.
The ungeneralised form of the specific line in question is:
Zr = str2num(char(strtok(clusters.Zr_Ranged,'%'))); % Gets numerical data from column in table
and the generalised form is:
element1 = char(elements{2,1}); % Pulls label from txt file
element1 = strcat('clusters.',element1,'_Ranged'); % Sets string for next line
element1 = str2num(char(strtok(element1,'%'))); % To get numerical data from table
but with this code, an empty array is returned (both are double precision arrays).
1 Comment
Walter Roberson
on 30 Aug 2017
When you say table, do you mean the MATLAB table() data object type? What you coded reminds me more of a struct than a table
Answers (1)
Peter Perkins
on 31 Aug 2017
Hard to tell what you are doing because you have not said what elements or clusters is.
It looks like you created your own way to "eval" a subscripting expression. Maybe you ought to be using some form of "dynamic field name" indexing. You said "table", so ...
>> t = table([1;2;3],[4;5;6])
t =
3×2 table
Var1 Var2
____ ____
1 4
2 5
3 6
>> varName = 'Var1';
>> t.(varName)
ans =
1
2
3
... but the same thing works for structs too.
0 Comments
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!