필터 지우기
필터 지우기

How to obtain variables from a structure inside a for loop?

조회 수: 4 (최근 30일)
Meghana Balasubramanian
Meghana Balasubramanian 2019년 8월 26일
댓글: Rik 2019년 8월 26일
Hello everyone, I have a code to call intensity values which are stored in a structure 's'. The structure 's' has various fields and the field(or variable, I'm not sure what it's called) signal2 has the values I need to access. When I use the following code for just one of the signal2 data's I get the desired output :
Code:
int_val= s.cellList.meshData{1,19}{1,1}.signal2;
int_val(int_val>threshold)=1;
int_val(int_val~=1)=0;
Output:
list of numbers in an excel sheet
({1,19} is a cell inside the field meshData containing the desired values).
However, when I try to create a loop to access each of the cells(1:25),
for j=1:25
int_val= 's_',num2str(i),'.cellList.meshData{1,',num2str(j),'}{1,1}.signal2';
%this ends up creating a character array.
int_val(int_val>threshold)=1;
int_val(int_val~=1)=0;
How do I get the values from the structure?
  댓글 수: 2
Stephen23
Stephen23 2019년 8월 26일
Your examples are inconsistent: your first example shows a variable named s, then your example
's_',num2str(i),'.cellList.meshData{1,',num2str(j),'}{1,1}.signal2'
shows a variable named s_X for some integer X.: which of these is correct?
Also, why are you converting perfectly good indices into strings?
Meghana Balasubramanian
Meghana Balasubramanian 2019년 8월 26일
편집: Stephen23 2019년 8월 26일
's' is a structure that I've created. I'm accessing variables in the structure.
I realized I could directly write the command as:
int_val= s.cellList.meshData{1, j}{1,1}.signal2;
instead of this:
int_val= 's_',num2str(i),'.cellList.meshData{1,',num2str(j),'}{1,1}.signal2';

댓글을 달려면 로그인하십시오.

채택된 답변

Bjorn Gustavsson
Bjorn Gustavsson 2019년 8월 26일
It seems that you read in entire data-sets into a number of numbered struct-variables (presumably s_1, s_2,..., s_25). Dont do that (search for "dynamically generating variable names" or something like that and read the reasons). Instead
read everything into an array of structs. Then you'll have something like this:
s = % Your data-reading-function/script, now returning a struct array
for i1 = numel(s):-1:1
for i2 = size(s(i1).cellList.meshData,1):-1:1
int_val(i1,i2) = s(i1).cellList.meshData{1,i2}{1,1}.signal2;
end
end
int_val(int_val(:)<=threshold) = 0;
int_val(int_val(:)>threshold) = 1;
HTH

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by