I'm wondering if there is a convenient way to update a struct with the values of another struct in Matlab.
Here is the code, with the use of fieldnames
, numel
and a for
loop,
fn = fieldnames(new_values);
for fi=1:numel(fn)
old_struct.(fn{fi}) = new_values.(fn{fi});
end
Of course, I don't want to loose the fields in old_struct
that are not in new_values
, so I can't use the simple old_struct=new_values
.
Updating a struct is something we may want to do in a single short line in an interpreter.