Changing multiple structure vales

2 ビュー (過去 30 日間)
Drew
Drew 2012 年 11 月 4 日
I have a problem where I have a structure D where D.r = 33.75e-3;D.L = 143e-3;D.d=103e-3;D.m2=.5355;D.L1=45.9e-3;D.m3=.5110;D.Im2=1.59e-3;D.ncyl=2;D.m1=0;D.r1=0;D.Im1=0;. I want to create a new strucure D1 so that D1 = D except for D1.r,D1.L,D1.d, and D1.m2 are changed. So that, D1.r = D.r + (D.r.*10e-6), D1.L = D.L + (D.L.*10e-6), D1.d = D.d + (D.d.*10e-6), D1.m2 = D.m2 + (D.m2.*10e-6). Is there a way to do this using a for loop or do I have to change each one individually?
  1 件のコメント
Walter Roberson
Walter Roberson 2012 年 11 月 4 日
Your question is difficult to read. Consider adding line breaks and markup.

サインインしてコメントする。

回答 (1 件)

Akiva Gordon
Akiva Gordon 2012 年 11 月 4 日
% Setup structure "S"
S.a = 11;
S.b = 22;
S.c = 33;
S.d = 44;
% This is the list of fields in "S" that I want to change
fields = {'c', 'd'};
for i = 1:numel(fields)
% Use dynamic field references to set these fields
S.(fields{i}) = S.(fields{i})*(1+1e-5);
end

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by