フィルターのクリア

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

13 ビュー (過去 30 日間)
Rui Zhang
Rui Zhang 2023 年 10 月 6 日
コメント済み: Rui Zhang 2023 年 10 月 9 日
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 Center および File ExchangeStructures についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by