How to find index of string inside a structure of cell arrays

10 Ansichten (letzte 30 Tage)
Andrew Tilmouth
Andrew Tilmouth am 26 Nov. 2022
Kommentiert: Paul am 26 Nov. 2022
I have a structure where the order of the parameter names in the names field can change and I want to extract the data from the values field associated with the name of interest in the names field. I have seen posts on this that work when the names in the names field are type ch as shown by the little icon at the top of the column when viewed in the data viewing window but for my structure the type is not ch but it is {}. Note I did not write the code that creates the structure and I don't want to start changing that code.
So far the posts I have read don't work and I can't work it out. I have attached a significantly cut down version of the structure. So for example I would like to get the index of the name 'ECU_FPC' in the names field and then get the 1x1x2 double data associated with ECU_FPC from the values field. Can anyone help me, no doubt so far I have just not got the right syntax!, thanks in advance

Akzeptierte Antwort

Paul
Paul am 26 Nov. 2022
Hi Andrew,
Is this what you're looking for?
load(websave('MyStruct.mat','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1208053/MyStruct.mat'));
MyStruct
MyStruct = 1×3 struct array with fields:
var names values
values = MyStruct(strcmp('ECU_FPC',string({MyStruct.names}))).values
values =
values(:,:,1) = 5.4400 values(:,:,2) = 5.5631
  2 Kommentare
Andrew Tilmouth
Andrew Tilmouth am 26 Nov. 2022
Awesome, thanks so much for your help
Paul
Paul am 26 Nov. 2022
HTH. Using a relational operator is also an option and might be a bit clearer.
load(websave('MyStruct.mat','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1208053/MyStruct.mat'));
MyStruct
MyStruct = 1×3 struct array with fields:
var names values
values = MyStruct(('ECU_FPC'==string({MyStruct.names}))).values
values =
values(:,:,1) = 5.4400 values(:,:,2) = 5.5631

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by