Contextmenu Callback to set class variable

2 ビュー (過去 30 日間)
Jonas Reber
Jonas Reber 2011 年 5 月 31 日
Hello MatLabers
I am looking for a better/smoother way to implement the setting of a class variable in a callback. I'm sure there is a nicer method than what I'm doing right now.
I have a class property called "displayMode" that has its own set method in the class (function set.displayMode(obj, val)...). On a contextmenu callback I would like to change that value.
% helper function to set the value of displayMode (class property)
function enableView(this, value, varargin)
this.displayMode = value;
end
% different function handles for callback:
en3D = @(h,e)enableView(this,'3D');
en2D = @(h,e)enableView(this,'Frame');
% the contextmenu
this.mycontextmenu = uicontextmenu;
item1 = uimenu(this.mycontextmenu,'Label','...', 'Callback', ...);
item2 = uimenu(this.mycontextmenu,'Label','3D view','Callback',en3D);
item3 = uimenu(this.mycontextmenu,'Label','2D view','Callback',en2D);
..
I'm looking for a possibility that avoids the additional function (here: enableView(...)). I'm thinking of something like:
hSet3D = ['set(this, ''displayMode'', ''3D'')'];
item3 = uimenu(this.mycontextmenu,'Label','better','Callback',hSet3D);
but I can not access the object itself here (this).
Thanks!

採用された回答

Titus Edelhofer
Titus Edelhofer 2011 年 5 月 31 日
Hi,
not really. You could use the setfield function for this purpose:
item3 = uimenu(this.mycontextmenu, 'Label', 'better', ...
'Callback', @(h,e) setfield(this, 'displayMode', 'Frame'));
I leave it up to you to decide, if you would call this better then your solution ;-). If I were you I would add a method setDisplayMode to the class that I would call in the callback...
Titus

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by