Changing multiple structure vales

2 views (last 30 days)
Drew
Drew on 4 Nov 2012
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?

Answers (1)

Akiva Gordon
Akiva Gordon on 4 Nov 2012
% 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

Community Treasure Hunt

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

Start Hunting!