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

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

If class name is classname1 and it has multiple subclasses:
classname1.subname1.subname2.variable1;
Let's stop right there. You're using the words "class" and "subclass" here but I'm pretty sure you're not using them the way MATLAB defines them.
Please show us a concrete example, something we can run, of your classname1 variable.
Even if I'm incorrect and you do literally mean converting an object with properties that themselves contain objects into a struct (with the fields that represent properties that contained objects instead containing structs created from those objects) converting objects to structs in MATLAB is discouraged. MATLAB issues a warning when you do this telling you that you probably shouldn't. You would no longer be able to use those structs as objects (calling methods of the class on them wouldn't work anymore) and you'd expose all the internal implementation details stored as properties of the object to public view.
So what is your purpose in trying to perform this conversion? What are you hoping to do with the struct you create? There may be a better way to achieve that goal without converting the object into a struct.
Below is an example:
myTest is an object of class zr1. It has three members:
(1) zrObj1 is an object of class zr2
(2) zrObj2 is an object of class zr3;
(3) zrVar1 is a variable with value of 4
Below are the 4 class definitions:
The class zr2 has a member:
(1) zrObj1 that is an object of class zr5
My goal is to construct struct "zrStruct" it has 3 members of struct or variable with the same name as myTest:
(1) zrObj1 (struct)
(2) zrObj2 (struct)
(3) zrVar1 (leave it there, doesn't need conversions)
Below shows the conversion result: zrStruct has three members with the same name. Instead of class objects, they are struct.
The script above for the conversion works only when I know all the field names of the class ahead. If I don't know their names and let the matlab script to find one by one, how can I do this?
The reason I do this is: we saved both simulation results and model meta data in a MAT file. The model meta data is in classes as I showed above like class myTest.
In order to access the model meta data when doing post processing, I need all the class defintions to access the meta data. I would like to develop script that can convert class hierarchy into struct hierarchy and save it in the MAT file. So later on, I can check the model meta data without class definitions.
This will bring me the following benefits:
If I process simulation results that run two years ago, now some of the classes changed a little bit, In order to get the meta data, I have to keep all the previous version of class definitions and have to know which version of the classes were used to run the simulation so I can get the right data
If the meta data were saved in struct, no matter when I want to get the meta data of the model, I can easily get them.
Is it guaranteed that this will only ever involve value classes?
I think this will need all the classes should implement method "saveobj()" . It is out of my control. There is a big community in the company and everyone could define their own classes and share them within the group.
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.
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

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!