Multiply structure by a constant
23 ビュー (過去 30 日間)
古いコメントを表示
Like to multiply a structure with fields by a constant, but it giving an error. Is there another way to do this?
0 件のコメント
回答 (2 件)
Bora Eryilmaz
2023 年 1 月 12 日
You have to cycle through the individual fields of the structure:
s = struct('a', [1 2 3 4], 'b', -1)
for f = fieldnames(s)'
s.(f{1}) = 4 * s.(f{1});
end
s
2 件のコメント
Bora Eryilmaz
2023 年 1 月 12 日
Not sure what you mean by "variable names". Are you referring to the variable name of the struct or the fields of the struct?
参考
カテゴリ
Help Center および File Exchange で Structures についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!