Multiply structure by a constant
18 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Like to multiply a structure with fields by a constant, but it giving an error. Is there another way to do this?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1261530/image.png)
0 Kommentare
Antworten (2)
Bora Eryilmaz
am 12 Jan. 2023
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 Kommentare
Bora Eryilmaz
am 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?
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!