[SOLVED ALREADY] Loop through data struct with variable names

조회 수: 59 (최근 30일)
Sokratis Panagiotidis
Sokratis Panagiotidis 2022년 7월 14일
댓글: Voss 2022년 7월 14일
EDIT:
Hi, So it seems like I was able to solve it already so yeah, Imma post the solution as an answer for people to look into it in case they have a similar problem.
Hi,
I am trying to loop through a structure called 'Erruptions'.
Now in this structure I have 3 more sub structs, thus it's length is 3.
The 3 structures have variable names 'E1', 'E2', etc. These are simply 1x2 doubles or vectors if you wanna call them that.
So by calling one of the structs I write
Erruptions.sub_struct
and I get its content
E1: [6.0014 1.0161]
E2: [4.5936 0.5191]
E3: [3.2693 0.2849]
E4: [2.7640 -0.0073]
E5: [2.3443 0.1345]
But when I want to get the number of elements it says it's only 1, doesn't matter if I use
length(Erruptions.sub_struct) % gives me ans = 1
or
size(Erruptions.sub_struct) % gives me ans = [1 1]
which is very confusing. I want to run a loop through every substruct but since it only recognizes them as one single element, opposed to the above shown 5, itleave the substruct after taking the first into consideration.
How can I solve this problem?
  댓글 수: 3
Sokratis Panagiotidis
Sokratis Panagiotidis 2022년 7월 14일
Hey, Now I uderstand my confusion. You were right about the mix up and the solution with 'fieldnames' is the correct one I was also able to solve.
But how would it confuse more people if I post the solution I found, since also someone else (besides you) was able to answer it this way?
Steven Lord
Steven Lord 2022년 7월 14일
One analogy or mental model I think helps is that of a bookshelf. One bookshelf (struct) may have many shelves (fields) and each shelf may contain just one item or a series of items (data in the field.)

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

채택된 답변

Voss
Voss 2022년 7월 14일
편집: Voss 2022년 7월 14일
% creating a structure like yours
Erruptions = struct('sub_struct',struct( ...
'E1',[6.0014 1.0161], ...
'E2',[4.5936 0.5191], ...
'E3',[3.2693 0.2849], ...
'E4',[2.7640 -0.0073], ...
'E5',[2.3443 0.1345]))
Erruptions = struct with fields:
sub_struct: [1×1 struct]
% Erruptions.sub_struct is a scalar structure, so its length is 1
Erruptions.sub_struct
ans = struct with fields:
E1: [6.0014 1.0161] E2: [4.5936 0.5191] E3: [3.2693 0.2849] E4: [2.7640 -0.0073] E5: [2.3443 0.1345]
% you want to get the fields of Erruptions.sub_struct
fn = fieldnames(Erruptions.sub_struct)
fn = 5×1 cell array
{'E1'} {'E2'} {'E3'} {'E4'} {'E5'}
% and loop through those
for ii = 1:numel(fn)
Erruptions.sub_struct.(fn{ii})
end
ans = 1×2
6.0014 1.0161
ans = 1×2
4.5936 0.5191
ans = 1×2
3.2693 0.2849
ans = 1×2
2.7640 -0.0073
ans = 1×2
2.3443 0.1345
  댓글 수: 2
Sokratis Panagiotidis
Sokratis Panagiotidis 2022년 7월 14일
Yeah this was pretty much the solution I was able to find only two minutes after posting that question except for using 'length' instead of 'numel'.
Thanks !
Voss
Voss 2022년 7월 14일
You're welcome!

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

추가 답변 (2개)

Benjamin Thompson
Benjamin Thompson 2022년 7월 14일
You want to index into the structure array, then pass one of those array elements to the length function:
>> S1.E1 = [1 1];
>> S2.E1 = [1 1];
>> SArray(1) = S1
SArray =
struct with fields:
E1: [1 1]
>> SArray(2) = S2
SArray =
1×2 struct array with fields:
E1
>> length(SArray.E1)
Error using length
Too many input arguments.
>> length(SArray(1).E1)
ans =
2
  댓글 수: 1
Stephen23
Stephen23 2022년 7월 14일
A much simpler way to create that structure:
S(1).E1 = [1,1];
S(2).E1 = [0,1];

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


Sokratis Panagiotidis
Sokratis Panagiotidis 2022년 7월 14일
SOLUTION:
Just like stated above I was able to solve it so here's ONE solution I managed to find.
What I did is I took the names of the struct, meaning
fn = fieldnames(struct);
and in the next step created a for loop
for j = 1:length(fn{j})
and THEN I did the exact same thing and took the fieldnames of each substruct, meaning
newfn = fieldnames(struct.(fn{j}));
which is
E1: [6.0014 1.0161]
E2: [4.5936 0.5191]
E3: [3.2693 0.2849]
E4: [2.7640 -0.0073]
E5: [2.3443 0.1345]
Now I try to get its length and voilà! I can use it in aother for loop to continue:
for i = 1:length(newfn)
So yeah, there you have it!

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by