Extract values from a deeply nested Struct for a designated fieldname

버전 1.2.0.0 (6.55 KB) 작성자: Yung-Yeh Chang
Useful tool for searching and collecting values in a tree data type STRUCT with specific fieldname
다운로드 수: 353
업데이트 날짜: 2017/6/12

라이선스 보기

STRUCTVAL is a convenient tool that extracts values from a deeply nested Struct for a designated fieldname.
Assuming you have a data set that is constructed with the same property and stored in a struct variable. It may look like:
data.a001.r01.values
data.a001.r02.values
data.a001.r03.values
data.b002.r01.values
data.c003.values
..
..
How can you extract all the values in `values` filed under each subset of `data`?

This is my solution.

[output] = STRUCTVAL(S,fieldname) deeply searches a STRUCT variable for the field values in `fieldname` and collects result in `output`. This function collects any data type but STRUCT. If the data type for the particular field is a struct, it keeps searching down further.

Furthermore, STRUCTVAL is also capable to reconstruct the hierarchical data structure! This is extremely useful when you need to replace a particularly field with new value!

For more information, type "help structval" in the command window to find out.
Example:
>> S.a = 1;
>> S.a.val = 1;
>> S.b.val = 'test';
>> field = 'val';
>> val = structval(S,field)
val =
[1] 'test'

This function is very useful when your data are hidden deeply down a nested STRUCT variable. I used it for collecting results from a Simulink output, which has the form such like data.resultA.signals.values, data.resultB.signals.values....and so forth. Hopefully, it will be useful to you. If you found any problem in the code, please don't hesitate to contact me or leave comments below.

Yung-Yeh Chang Ph.D.
6/12/2017

인용 양식

Yung-Yeh Chang (2024). Extract values from a deeply nested Struct for a designated fieldname (https://www.mathworks.com/matlabcentral/fileexchange/50534-extract-values-from-a-deeply-nested-struct-for-a-designated-fieldname), MATLAB Central File Exchange. 검색됨 .

MATLAB 릴리스 호환 정보
개발 환경: R2017a
모든 릴리스와 호환
플랫폼 호환성
Windows macOS Linux
카테고리
Help CenterMATLAB Answers에서 Structures에 대해 자세히 알아보기
태그 태그 추가

Community Treasure Hunt

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

Start Hunting!
버전 게시됨 릴리스 정보
1.2.0.0

Fix "Dimensions of matrices being concatenated are not consistent" error for some deeply nested struct.

1.1.0.0

Update help description and examples
Update description

1.0.0.0