Updating many field values at once
1 view (last 30 days)
Show older comments
PANAGIOTIS GEORGIOS ILIOPOUOS
on 3 Jun 2022
Answered: Walter Roberson
on 3 Jun 2022
I have a stuct with many fields: How can i update their values at once and not manually?
manually:
data.speed =data.speed(updated_values)
data.power=data.power(updated_values)
data.fuel=data.fuel(updated_values)
data.trim=data.trim(updated_values)
data.draft=data.draft(updated_values)
There is any way to do this automatically?
1 Comment
Walter Roberson
on 3 Jun 2022
Does it happen to be the case that all fields are being changed the same way? If so then structfun()
Accepted Answer
Jan
on 3 Jun 2022
F = fieldnames(data);
for k = 1:numel(F)
data.(F{k}) = data.(F{k})(updated_values);
end
0 Comments
More Answers (1)
Walter Roberson
on 3 Jun 2022
If all of the fields are being updated
data = structfun(a(M) M(updated_values), data, 'uniform', 0)
0 Comments
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!