Updating many field values at once

1 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2022-6-3
Does it happen to be the case that all fields are being changed the same way? If so then structfun()

请先登录,再进行评论。

采纳的回答

Jan
Jan 2022-6-3
F = fieldnames(data);
for k = 1:numel(F)
data.(F{k}) = data.(F{k})(updated_values);
end

更多回答(1 个)

Walter Roberson
Walter Roberson 2022-6-3
If all of the fields are being updated
data = structfun(a(M) M(updated_values), data, 'uniform', 0)

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by