3.66667

3.7 | 3 ratings Rate this file 23 downloads (last 30 days) File Size: 2.11 KB File ID: #18816

Nested Structure to Flat Cell Vector

by Hoi Wong

 

18 Feb 2008 (Updated 20 Feb 2008)

No BSD License  

Tools to flatten a nested struct into cell

Download Now | Watch this File

File Information
Description

flatNestedStructAccessList.m:
=======================
Create a string list of all possible structure access operators (like 'x.y.z')

flattenNestedStruct.m
===============
Uses flatStructAccessList.m to flatten a nested structure into a column of cells.

NOTE
====
This is NOT a GUI program despite what you see in the screenshots. The screenshot how it was used for my project.

I haven't tested this program much except in my project. Please report any bug to wonghoi.ee@gmail.com.

MATLAB release MATLAB 7.5 (R2007b)
Zip File Content  
Other Files flattenNestedStruct.m,
flatNestedStructAccessList.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (4)
19 Feb 2008 Duane Hanselman

This might be useful, but being a 23+ year MATLAB user, I can't figure out what it does from the help text. I am especially troubled by the appearance of the function eval being called within a for loop. Would it make sense to make flatStructAccessList to be a subfunction of flattenStruct? Does this do what the builtin function struct2cell already does? What is meant by saying "No vector (or cell vector) allowed anywhere in the input struct!"?

19 Feb 2008 Hoi Wong

Thanks Duane for the useful comments. I actually thought of spending more time on the help file, but I just find it quite difficult to describe what the program does without an example screenshot that has the structure displayed as a tree. I'll post a screen shot in a day or two, and I'd appreciate any help describing what this program does in simple words.

struct2cell() works for one level structures only. Hence if you have a structed formed by

a.b.c.d = 1
a.p.q.r = 2

struct2cell() will still give you cells that contains structs, which is not what I wanted (basically I want to form a table from a nested struct).

Also, in this context, since I want a list of all possible leaf nodes in a nested struct, using anything other than feval will make the program extremely clumsy. Please let me know if anybody has a better idea :)

I intentionally separate flatStructAccessList() from flatStruct() because some people might just want a list (like tree view) without the computational work putting all the contents into a cell vector.

By the way, "No vector (or cell vector)" should be written as "No non-singleton struct branch allowed". For structs, it's possible to have something like:

a{1}.b=1
a{2}.c=3
p(1).q=2
p(2).q=4

In this case, 'a' is a vector of cells which all the elements are structs, and 'p' is a vector which all the elements are structs. I don't know how to describe them in simple words, so please help!

If you found a good way to describe it, send me a email at wonghoi.ee@gmail.com.

21 Apr 2008 e lee

I found this very handy, I wanted to do a simple check if a nested structure field existed or not, but it seems overly difficult to do this (exist doesn't work for nested structure fiels, isvar works for one level of nesting, etc.)
I was able to do a strcmp on the output to find if the nested structure field exists.

I would also really like to have a gui (like what is shown) that lets you see and edit the values of the structure easily. I would love to have a "tree grid" where nodes could be collapsed (similar to what you show on the right part of the screen shot, with an additional column for the values. But, maybe I am dreaming.

For handling "non-singleton struct branch" perhaps you can find some hints in the Geodise "xml toolbox", it seems to be able to parse pretty complicated matlab structures into xml.

Regards,
Eric

07 Jan 2009 Ofek Shilon

Exactly what i needed, works like charm. thanks!

Please login to add a comment or rating.
Updates
20 Feb 2008

Added screenshots.

Renamed files so people won't mistaken it for struct2cell()

Updated help text. You still need to rely on the screenshots to understand it though.

Tag Activity for this File
Tag Applied By Date/Time
structures Hoi Wong 22 Oct 2008 09:48:12
cell arrays Hoi Wong 22 Oct 2008 09:48:12
nested struct flat cell Hoi Wong 22 Oct 2008 09:48:12
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com