How to convert matlab class with subclass to a struct with substruct?

15 views (last 30 days)
If class name is classname1 and it has multiple subclasses:
classname1.subname1.subname2.variable1;
name1.subname3.variable2;
...
How can I convert the class with all of its subclass and variables into struct with the same class name and variable names as below:
structname1.subname1.subname2.variable1;
structname1.subname3.variable2;
...
I tried struct() function in the following way:
structname1 = struct(classname1). it converts only the top level of the class into a struct but leaves subname1, subname2 in class objects, not struct.
  6 Comments
Walter Roberson
Walter Roberson on 7 Oct 2023
If the classes are permitted to be handle classes, then there is the possibilities of loops. If the classes are permitted to have properties that are graphics objects, then loops become very likely.
I do have code that can descend through structs / cells / objects. I wrote it a couple of weeks ago to search throughly into data to find given strings. A variation on the technique could be used to chase right down through and convert objects to structs, even in situations like a cell array containing a non-scalar struct that has a field that has an object -- my recent code is robust enough to handle such situations.
However, my recent code had anti-looping self-projection built in to skip 'Parent', 'Parent_I', 'NodeParent', 'NodeParent_I' . Also, in the form I wrote it, my recent code only processes any given handle once, simply skipping it when it shows up again... whereas for your purposes, you would want something closer to caching the results of processing any given handle so that if the handle shows up again, substitute the cached version.
Rui Zhang
Rui Zhang on 9 Oct 2023
Thank you, Walter Roberson, for following up this.
I think it is being resolved now and I am testing it.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 6 Oct 2023
This cannot always be done.
The graphics containers such as figures and axes are classes that contain graphic objects. The most common graphics objects are classes that have a Parent property.
You might argue that is outside the scope of your request because Figure and Axes are different classes not subclasses, but clearly it is easy to create a subclass that contains a handle to a parent.

More Answers (0)

Categories

Find more on Historical Contests in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!