loop over struct element
Show older comments
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.
Answers (1)
Walter Roberson
on 29 Apr 2018
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.
Categories
Find more on Structures in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!