Indexing a struct inside a struct

14 views (last 30 days)
Fabrizio
Fabrizio on 2 Aug 2023
Commented: Steven Lord on 2 Aug 2023
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 Comment
Stephen23
Stephen23 on 2 Aug 2023
@Fabrizio: please upload your data in a MAT file, by clicking the paperclip button.

Sign in to comment.

Answers (1)

Catalytic
Catalytic on 2 Aug 2023
Edited: Catalytic on 2 Aug 2023
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 Comment
Steven Lord
Steven Lord on 2 Aug 2023
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.

Sign in to comment.

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!