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

9 次查看(过去 30 天)
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

采纳的回答

Paul
Paul 2022-11-26
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 个评论
Paul
Paul 2022-11-26
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by