How to extract variable data from a table
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2017-8-30
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
回答(1 个)
Peter Perkins
2017-8-31
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 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!