set the value of multiple fields in an existing struct

3 vues (au cours des 30 derniers jours)
Toolman Thoolen
Toolman Thoolen le 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

Réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by