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

9 views (last 30 days)
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

Accepted Answer

Paul
Paul on 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 Comments
Paul
Paul on 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

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!