How to convert matlab class with subclass to a struct with substruct?
Show older comments
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
Steven Lord
on 6 Oct 2023
Moved: Matt J
on 6 Oct 2023
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.
Rui Zhang
on 6 Oct 2023
Walter Roberson
on 6 Oct 2023
Is it guaranteed that this will only ever involve value classes?
Rui Zhang
on 6 Oct 2023
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
on 9 Oct 2023
Accepted Answer
More Answers (0)
Categories
Find more on Historical Contests in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


