Using structfun with nested structures
30 ビュー (過去 30 日間)
古いコメントを表示
Hi everyone,
I have an overarching structure, with nested substructure, in the following format:
overarching(k).nested
with k=1:8. These nested structures have different sizes, and I was trying to compute the size of each nested structure using structfun as follows:
nested_size=structfun(@size,overarching,'UniformOutput',0)
However, the input structure is non-scalar, and it does not work. I can get the size of each nested structure using a loop to iterate on the nested structures, but I would like to avoid loops if possible. Is there a way of doing this using structfun, that I am not aware of?
Thank you in advance for your help!
0 件のコメント
採用された回答
Steven Lord
2016 年 9 月 7 日
The structfun function only works on scalar struct arrays, iterating over the fields of that scalar struct. While you could use arrayfun, it may be easier to understand (and perform just as well) if you use a for loop. I know people have said "Don't use for loops in MATLAB, they're slow." While that may have been true in the past (and you can still write a for loop so it is slow in current MATLAB) the performance of for loops have increased significantly with the improvements we have made to MATLAB over the years.
Besides a for loop over 8 elements of a struct array calling size is highly unlikely, in my experience, to be the bottleneck in your code.
0 件のコメント
その他の回答 (1 件)
Adam
2016 年 9 月 7 日
編集済み: Adam
2016 年 9 月 7 日
You should be able to wrap it in an arrayfun. This is likely slower than using a for loop, as arrayfun tends to be, but if you are determined not to use a loop it should do the job. Personally I prefer arrayfun to a for loop where speed is not an issue because it looks neater to me, but it is a matter of preference when it comes to that.
If you can upload a sample of your structure I can test it to get the syntax, but I don't have time to be trying to create the right kind of nested structures myself to test right now.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Structures についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!