How to extract variable data from a table

조회 수: 3 (최근 30일)
Philip Edmondson
Philip Edmondson 2017년 8월 30일
답변: Peter Perkins 2017년 8월 31일
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
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
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.

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by