Multiply structure by a constant
Afficher commentaires plus anciens
Like to multiply a structure with fields by a constant, but it giving an error. Is there another way to do this?

Réponses (2)
s.a=1; s.b=2,
S=structfun(@(f)10*f,s,'uni',0) %multiply by 10
You have to cycle through the individual fields of the structure:
s = struct('a', [1 2 3 4], 'b', -1)
for f = fieldnames(s)'
s.(f{1}) = 4 * s.(f{1});
end
s
2 commentaires
rockstar49
le 12 Jan 2023
Bora Eryilmaz
le 12 Jan 2023
Not sure what you mean by "variable names". Are you referring to the variable name of the struct or the fields of the struct?
Catégories
En savoir plus sur Structures dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!