Handle to object method

Ok, so I have this object Object1. One of its properties is another object, Object2 : Object1.prop = Object2.
Now from within a method of Object1, I am creating GUI controls whose callbacks I want to be one of Object2's public methods, say Object2.publicMethod.
So in Object1 I have something of the form:
function callbackWillBeCreated(this)
guicontrol('Callback', @this.prop.publicMethod);
end
This doesn't work ! When the callback is called, Matlab sends an error saying it doesn't know the function.
Does anyone have a fix or an explanation as to why it doesn't work ?
Thanks

 採用された回答

Matt J
Matt J 2012 年 10 月 5 日
編集済み: Matt J 2012 年 10 月 5 日

0 投票

What you had would be fine if you have R2012a or later, which I guess you don't.
But for earlier MATLAB versions, it won't work because "this.prop.publicMethod" is not the name of a function. You will need to do something like this,
guicontrol('Callback', @() this.prop.publicMethod);
Or, if the callback takes arguments
guicontrol('Callback', @(x) this.prop.publicMethod(x));
and so on.

2 件のコメント

Ivan
Ivan 2012 年 10 月 5 日
編集済み: Ivan 2012 年 10 月 5 日
This should work, yes. I'll try. But I do have 2012a. Will install 2012b and see if that's better.
Thanks for the quick reply !!
Matt J
Matt J 2012 年 10 月 5 日
Before you upgrade, try this in R2012a:
function callbackWillBeCreated(this)
prop=this.prop;
guicontrol('Callback', @prop.publicMethod);
end
I think it's just a problem of how much nested indexing you can have.

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

その他の回答 (0 件)

カテゴリ

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

質問済み:

2012 年 10 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by