loop over struct element
显示 更早的评论
Suppose I create a struct
data.ID0 data.ID1
Suppose I want to have a loop i that first work with data.IDO and then with data.ID1. Please advise.
回答(1 个)
Walter Roberson
2018-4-29
structfun(). If the results are scalar numeric values and what you want is an array of the results, then do not use the 'uniform', 0 option. Otherwise use 'uniform', 0, which will cause a struct to be returned with fields the same as the original field names, but with content as generated by the function you specified.
Example:
structfun(@mean2, data) %mean2 always returns a scalar even for empty input
structfun(@tan, data, 'uniform', 0) %returns struct with fields ID0 and ID1 in which each value has been replaced by its tan.
类别
在 帮助中心 和 File Exchange 中查找有关 Structures 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!