extract data from cell array
1 回表示 (過去 30 日間)
古いコメントを表示
%Lets say I have a multi-element cell array with different variables/feilds within
c{1,1}.name = 'Tom'; c{1,1}.age = 48;
c{2,1}.name = 'Bob'; c{2,1}.age = 24;
c{3,1}.name = 'Jim'; c{3,1}.age = 12;
%I would like to create a vector, vec_age = [48 24 12]; from this data set
% without writing the loop
for i =1:length(c), vec_age(i) = c{i,1}.age; end
% Something nice would be similar to how you pull a vector from a matrix, v = m(:,3);
% in the form of vec_age = c{:,1}.age; ... but alas this does not work
% Thank you in advance for suggestion to a small problem that has been nagging me.
0 件のコメント
採用された回答
Walter Roberson
2022 年 1 月 24 日
c{1,1}.name = 'Tom'; c{1,1}.age = 48;
c{2,1}.name = 'Bob'; c{2,1}.age = 24;
c{3,1}.name = 'Jim'; c{3,1}.age = 12;
vec_age = cellfun(@(S) S.age, c)
This does not assume that the structs all have the same fields in the same order, which Benjamin's answer assumes.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Tables についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!