How to find index of string inside a structure of cell arrays
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Andrew Tilmouth
el 26 de Nov. de 2022
Comentada: Paul
el 26 de Nov. de 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
0 comentarios
Respuesta aceptada
Paul
el 26 de Nov. de 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
values = MyStruct(strcmp('ECU_FPC',string({MyStruct.names}))).values
2 comentarios
Paul
el 26 de Nov. de 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
values = MyStruct(('ECU_FPC'==string({MyStruct.names}))).values
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!