Accessing structure by its name and looping through it

조회 수: 1 (최근 30일)
Eric Christensen
Eric Christensen 2019년 9월 18일
댓글: Stephen23 2019년 9월 19일
Hi,
I'm wondering how I can access a structure by its upper level name from a cell array, and loop through it. For example:
Tests = {'Cold Run'
'Warm Run'
'Hot Run'};
for I = 1:length(Tests)
plot(Tests{I}.Time,Tests{I}.Speed);
hold on;
end
Tests{I} is wrong. If it was part of a larger structure, say "Something", then this would work:
Something.(Tests{I}).Time
But what is the proper syntax to do this without an upper structure?
  댓글 수: 5
Eric Christensen
Eric Christensen 2019년 9월 18일
Ah yes, I overcomplicated it by creating that cell array just to have the names to loop through. That seems to work, thanks.

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

채택된 답변

Matt J
Matt J 2019년 9월 19일
Why not organize the data as a struct array?
Tests(1).Time = data1;
Tests(1).Speed = data2;
Tests(2).Time = data3;
Tests(2).Speed = data4;
Tests(3).Time = data5;
Tests(3).Speed = data6;
The indexing of the different scalar structures Tests(j) is now immediate.

추가 답변 (0개)

카테고리

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