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

조회 수: 7 (최근 30일)
Andrew Tilmouth
Andrew Tilmouth 2022년 11월 26일
댓글: Paul 2022년 11월 26일
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
Andrew Tilmouth
Andrew Tilmouth 2022년 11월 26일
Awesome, thanks so much for your help
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