Is it possible to access all nested structures in symmetrical struct tree?

10 ビュー (過去 30 日間)
Jostein Holmgren
Jostein Holmgren 2017 年 3 月 7 日
回答済み: Rajanya 2024 年 11 月 18 日
I have a struct looking something like this:
struct.nested1.data
struct.nested2.data
struct.nested3.data
Is there a way to access all .data nested structures? Similar to how in UNIX you can do something like
ls folder/*/subfolder
to list the contents of all subfolders.

採用された回答

Rajanya
Rajanya 2024 年 11 月 18 日
You can use ‘structfun’ for this purpose. It applies a function to each field in a scalar structure. For example, the ‘data’ members to an example nested structure ‘struct’ can be listed out as follows:
struct.nested1.data = 'a';
struct.nested2.data = 'b';
struct.nested3.data = 'c';
structfun(@(x) disp(x.data), struct);
a b c
For more details on the various ways to use ‘structfun’, you can refer to the documentation by running the following command in the MATLAB Command Window:
doc structfun
Hope this helps!

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by