How to register a class method as a figure callback?

조회 수: 3 (최근 30일)
Bradley Stiritz
Bradley Stiritz 2013년 7월 16일
I need to register a classdef method as a figure callback. I've associated the class object with the figure, but I'm not sure how to reliably call a class method in response to a UI control? Option 1 below works, but I'm afraid this won't be robust to state changes within the class object. Option 2 generates a syntax error.
I'm sure there must be a way to use the @ operator to specify a class method. Could someone please help me with this? Am I correct that Option 2 is safer than Option 1?
Thanks for your consideration.
function RenderFigure(obj)
% This is a method of classdef MyClass
% Create figure & store as an object property
obj.h_figure = figure(...)
% Associate the class object with the figure handle:
setappdata(obj.h_figure,'MyClass_object',obj)
% Register callback for a UI control within the figure. When the callback is executed, we want to call the class method respond_to_uicontrol() ..
% OPTION 1 : take snapshot of class object; hopefully its state hasn't changed by the time the callback is executed. Note that we don't have to pass the figure handle to the class method, since it's available to the method as an object property.
uicontrol(...
...
'Callback',@(h,e)obj.respond_to_uicontrol() ...
);
% OPTION 2 : retrieve class object & execute method. This generates a syntax error at the dot immediately following 'MyClass'
uicontrol(...
...
'Callback',@(h,e)feval(@MyClass.respond_to_uicontrol,...
getappdata(h,'MyClass_object') ...
);

채택된 답변

per isakson
per isakson 2013년 7월 21일
편집: per isakson 2013년 7월 21일
"OPTION 1 : take snapshot[...]hopefully its state hasn't changed[...]"
Changes of the object should not be a problem with an object of a handle class. It's the "handle" that is frozen in the snapshot, not the underlying object.
With value object - ???
  댓글 수: 2
Bradley Stiritz
Bradley Stiritz 2013년 7월 24일
Hi Per, thanks for your answer. I'm thinking you may be correct, but I'm double-checking with TMW tech support just to be sure. I'll respond & credit you once they confirm.
% OPTION 1
uicontrol(...
...
'Callback',@(h,e)obj.respond_to_uicontrol() ...
);
I don't understand how it can be the case that, as you say, only the handle is frozen in the snapshot. Semantically, option 1 refers to (obj), i.e. the entire underlying object, not simply to the object handle.
Bradley Stiritz
Bradley Stiritz 2013년 9월 22일
Per, thanks again for your answer. Sorry to have taken such a long time to wrap this up. TMW support confirmed that "option 1" works as desired.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by