How to get a complete list of class properties including private properties.

I wrote a class which have both public and private properties.
In a private mehod of the class, I need to get a complete list of porperties of the class, definitely, including all private properties.
Buit-in function 'properties' reveals only public properties.
What would be my options?
Thank you.

 採用された回答

Steven Lord
Steven Lord 2022 年 12 月 7 日

0 投票

Create a metaclass object and iterate through the PropertyList.

5 件のコメント

Jaeseok
Jaeseok 2022 年 12 月 7 日
編集済み: Jaeseok 2022 年 12 月 7 日
very quick & absolutley correct answer. Thank you.
> mc = metaclass(MYCLASS.empty);
> prop_list = {mc.PropertyList.Name};
Steven Lord
Steven Lord 2022 年 12 月 7 日
That would work as long as MYCLASS is not Abstract. But if you have an Abstract class (meaning you can't instantiate an instance of the class) see the second and third paragraphs in the Description section on the documentation page to which I linked.
Rajmohan
Rajmohan 2024 年 5 月 24 日
Unfortunately, metaclass is on this list of matlab functions supported for code generation.
Are there any suggestions on how this could be accomplished for an application that requires code generation?
Thanks :)
Steven Lord
Steven Lord 2024 年 5 月 24 日
I think you meant that metaclass is not supported for code generation.
Since the dynamicprops Class is also not listed as having support for code generation, I believe the complete list of properties should be known at the time the code is generated. So if worst comes to worst you could hard-code that list of properties in a method of your class.
I'm not sure what your use case is for dynamically generating the list of properties of a class implemented in MATLAB code inside a C or C++ file generated from that class. That smells kind of fishy to me.
Rajmohan
Rajmohan 2024 年 5 月 28 日
You are correct, I meant metaclass is not supported for code generation.
You probably have a better programming solution compared to mine -
I am working on developing a matlab.System object. The input to this block, u, is [n,1] where it is possible to have different forms of inputs. i.e. it has to support some legacy input formats and some more recent iterations which means that n could be different and so their ordering.
My solution to generalize this was to define an input class which knows the order of inputs and assigns them to named properties - the goal being the order of the inputs is prescribed in one location only. example: obj.a = u(1); obj.b = u(2); if input if of type A. If input is of type B, then obj.a = u(17), obj.b = u(57) and so on based on the input class that was selected. This solution works great but can be a little slow as the stepImpl has a very large number of property calls. So my intention was to create a structure with the fields and values to help improve performance. Since the property list can change based on the input type, the easy way is to get the list of properties and create a structure.
Please feel free to tell me if that assumption for performance improvement is inaccurate.
I suppose a workaround would be to require that the input classes define a method to create a structure.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeArgument Definitions についてさらに検索

質問済み:

2022 年 12 月 7 日

コメント済み:

2024 年 5 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by