set the value of multiple fields in an existing struct

3 Ansichten (letzte 30 Tage)
Toolman Thoolen
Toolman Thoolen am 28 Mai 2019
Hello,
I have an existing structure and I would like to update a couple of fields with some new values.
Is there a way to do this without using a for-loop?
% Original struct
OLD = struct('field1',1,'field2',2,'field3',5,'field5',9)
% Struct containing the updates
NEW = struct( 'field2',7,'field3',8)
% Update original with loop
fn_NEW = fieldnames(NEW)
for i = 1:numel(fn_NEW)
OLD.(fn_NEW{i}) = NEW.(fn_NEW{i});
end

Antworten (0)

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by