How can I get the variable name of a child object?

6 ビュー (過去 30 日間)
Wesley Allen
Wesley Allen 2013 年 8 月 27 日
I want to display the name of an object variable in its "disp" method.
I currently do the following:
methods
function disp(this)
objVar = inputname(1);
disp(['Var: (' objVar ')']);
end
end
This works great for a lone object:
>> myObj = myClass();
>> disp(myObj)
Var: (myObj)
>>
However, for a child object, it does not work:
>> myParent = myParentClass();
>> myParent.child = myClass();
>> disp(myParent.child)
Var: ()
>>
How can I get the variable name to display for a child object?
e.g.
>> myParent = myParentClass();
>> myParent.child = myClass();
>> disp(myParent.child)
Var: (myParent.child)
>>

回答 (1 件)

Walter Roberson
Walter Roberson 2013 年 8 月 27 日
You cannot, at least not without some nastiness involving the program examining its own source code. "myParent.child" is not a variable name. "myParent.child" will be treated as an expression, and expressions that are not simple variable names will always come out empty for inputname() purposes.
  1 件のコメント
Wesley Allen
Wesley Allen 2013 年 8 月 27 日
That's what I was afraid of.

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

カテゴリ

Help Center および File ExchangeCreate System Objects についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by