필터 지우기
필터 지우기

Indexing a struct inside a struct

조회 수: 10 (최근 30일)
Fabrizio
Fabrizio 2023년 8월 2일
댓글: Steven Lord 2023년 8월 2일
Hello!
I need to index a struct inside a struct, and dot indexing is not supported.
I'll explain better what I mean.
Each struct is a session (called dataA, corresponding for instance to the Race), and inside each session there is another struct, which is a stint, and inside the stint struct there are 1x98 structs (for instance, each is one lap). Inside each lap struct I have the variables I need (for instance, vCar, velocity vector).
For each stint, I want to go through all the 98 laps, and for each lap i want to go through the entire vCar vector and see where the vector has values that make no sense (for instance, when vCar(ii) > 500, add this index to a vector so at the end i have a vector of all the laps with errors in them).
The code is below (I'm sure there are also faster way to do this, but the main problem here is the indexing).
Thank you!
Fabrizio
for jj = 1:length(dataA.Stint)
for ii = 1:length(dataA.Stint{1, jj}.D_vCar) % ERROR, i want to call lap jj
if dataA.Stint{1, jj}.D_vCar(ii) > 10000 % ii is the length of the velocity vector
error_laps = [error_laps; jj]
break;
end
end
end
ERROR:
Dot indexing is not supported for variables of this type.
Error in dataanalysis (line 12)
for ii = 1:length(dataA.Stint{1, jj}.D_ETyreSlidingFLTotal)
  댓글 수: 1
Stephen23
Stephen23 2023년 8월 2일
@Fabrizio: please upload your data in a MAT file, by clicking the paperclip button.

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

답변 (1개)

Catalytic
Catalytic 2023년 8월 2일
편집: Catalytic 2023년 8월 2일
If Stint is a struct array, as you say, then it should be indexed with parentheses (), not braces {},
dataA.Stint(jj)
Conversely, it would be better to store error_laps in a cell array (using braces) because you don't know what the length of the result will be. Overall, your code should probably be -
for jj = 1:numel(dataA.Stint)
error_laps{jj}= find(dataA.Stint(jj).vCar>1000 ,1);
end
  댓글 수: 1
Steven Lord
Steven Lord 2023년 8월 2일
Conversely, it would be better to store error_laps in a cell array (using braces) because you don't know what the length of the result will be.
Or perhaps a table array with one variable containing the stint number and another the lap number, with whatever data variables you want for each stint / lap combination (velocity, race position, etc.)
If the data is time-based (how long since the start of the race the measurement was taken) a timetable array may also be an option.

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

카테고리

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

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by