필터 지우기
필터 지우기

call out matlab structure using for loop

조회 수: 4 (최근 30일)
AI-CHI Chang
AI-CHI Chang 2022년 3월 9일
댓글: Stephen23 2022년 3월 10일
I have j structures Ts(j) . And the elements inside the structure are just like T(3) below
can I call out e1 e2 e3 by using for loop?
or I can only call they by using Ts(i).e1 Ts(i).e2 Ts(i).e3.
Because I need to check whether e1||e2||e3 third element is 1 or 2 , and now I have j structures.
If I can't use for loop ↓ ,then I ill have three " if "
for j=1:num_of_Ts
if Ts(j).e1(:,3)==1
do...
end
if Ts(j).e2(:,3)==1
do...
end
if Ts(j).e3(:,3)==1
do...
end
end

채택된 답변

Max Alger-Meyer
Max Alger-Meyer 2022년 3월 9일
You can absolutely use a loop! The trick is to use the 'fieldnames' function as shown below.
SampleStruct.e1 = [129 152 1];
SampleStruct.e2 = [129 164 2];
SampleStruct.e3 = [152 164 1];
names = fieldnames(SampleStruct);
for i = 1:numel(names)
disp(SampleStruct.(names{i}))
end
129 152 1 129 164 2 152 164 1
  댓글 수: 1
AI-CHI Chang
AI-CHI Chang 2022년 3월 10일
Thanks!! I think this is absolutely what I want

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by