Indexing a struct inside a struct

14 ビュー (過去 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 Center および File ExchangeStructures についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by