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

13 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2023-10-7
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 2023-10-9
Thank you, Walter Roberson, for following up this.
I think it is being resolved now and I am testing it.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2023-10-6
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by