How to extract the right struct from the cell array of structs based on condition?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Nazar Adamchuk
el 28 de Jun. de 2022
Respondida: Chunru
el 28 de Jun. de 2022
have a 4x1 cell array that looks like in the attached file.
load('part.mat');
searchedItem = struct();
for k = 1:length(values)
TF = isfield(values{k},{'property'});
if TF == 1
if convertCharsToStrings(values{k}.property) == "Mass %"
searchedItem = values{k};
end
end
end
I need to extract the struct with the fieldname Mass %. (not all of the structs have a fieldname property). What is the MATLAB-ish way to do it avoiding the loops? In my original file I have to deal not with 4x1 cell array but 200+.
0 comentarios
Respuesta aceptada
Chunru
el 28 de Jun. de 2022
load part.mat
whos
% MATLAB-ish way
idx = cellfun(@(x) isfield(x, 'property') && x.property == "Mass %", values);
searchedItem = values(idx)
searchedItem{1}
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Structures 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!