Changing multiple structure vales
1 次查看(过去 30 天)
显示 更早的评论
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
2012-11-4
Your question is difficult to read. Consider adding line breaks and markup.
回答(1 个)
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
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!