How to extract the right struct from the cell array of structs based on condition?
2 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Chunru
2022 年 6 月 28 日
load part.mat
whos
% MATLAB-ish way
idx = cellfun(@(x) isfield(x, 'property') && x.property == "Mass %", values);
searchedItem = values(idx)
searchedItem{1}
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Structures についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!