How can I manipulate with fields in structure.
25 ビュー (過去 30 日間)
古いコメントを表示
I have struct s with 96 fields that have different names, each field is 44x1 column of numbers.
is it possible to divide all numbers in each field by some number, or use reshape to make from 44*1 to 11*4 matrix, or for example find max from each field and make a new array from those maximum values.
0 件のコメント
採用された回答
Voss
2022 年 3 月 4 日
You can use structfun() for that:
% creating a struct like yours but with only 3 fields:
s = struct('field1',zeros(44,1),'field2',ones(44,1),'field3',randn(44,1))
% divide by 7:
s_new = structfun(@(x)x/7,s,'UniformOutput',false)
s_new.field2
% reshape:
s_new = structfun(@(x)reshape(x,11,4),s,'UniformOutput',false)
% get the max:
s_max = structfun(@max,s)
2 件のコメント
Voss
2022 年 3 月 4 日
You're welcome!
If that answers your question, please consider marking the answer as 'Accepted'. Thanks!
その他の回答 (1 件)
Jan
2022 年 3 月 4 日
A loop over the field names can achieve this:
s = struct('field1',zeros(44,1),'field2',ones(44,1),'field3',randn(44,1)); % Thanks _
Field = fieldnames(s);
for k = 1:numel(Field)
s.(Field{k}) = s.(Field{k}) / 17;
% Or: s.(Field{k}) = reshape(s.(Field{k}), 4, 11);
end
This is done in structfun also, but the loop runs faster and is easier to read, in my opinion.
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!