MATLAB에서 클래스 메서드가 공개되지 않고 uicontrol 콜백 역할을 할 수 있습니까?
-
01-07-2019 - |
문제
MATLAB 2008a에서는 메서드를 공개하지 않고도 클래스 메서드가 uicontrol 콜백 함수로 작동하도록 허용하는 방법이 있습니까?개념적으로 메서드는 클래스 사용자가 호출하면 안 되기 때문에 공개되어서는 안 됩니다.콜백을 트리거하는 UI 이벤트의 결과로만 호출되어야 합니다.그러나 메서드의 액세스를 비공개 또는 보호로 설정하면 콜백이 작동하지 않습니다.내 클래스는 hgsetget에서 파생되었으며 2008a classdef 구문을 사용하여 정의되었습니다.
uicontrol 코드는 다음과 같습니다.
methods (Access = public)
function this = MyClass(args)
this.someClassProperty = uicontrol(property1, value1, ... , 'Callback', ...
{@(src, event)myCallbackMethod(this, src, event)});
% the rest of the class constructor code
end
end
콜백 코드는 다음과 같습니다.
methods (Access = private) % This doesn't work because it's private
% It works just fine if I make it public instead, but that's wrong conceptually.
function myCallbackMethod(this, src, event)
% do something
end
end
해결책
콜백의 함수 핸들을 개인 속성으로 저장하면 문제가 해결되는 것 같습니다.이 시도:
classdef MyClass
properties
handle;
end
properties (Access=private)
callback;
end
methods
function this = MyClass(args)
this.callback = @myCallbackMethod;
this.handle = uicontrol('Callback', ...
{@(src, event)myCallbackMethod(this, src, event)});
end
end
methods (Access = private)
function myCallbackMethod(this, src, event)
disp('Hello world!');
end
end
end
제휴하지 않습니다 StackOverflow