2008-09-19 9 views
7

MATLAB 2008a에서 클래스 메서드가 메서드를 public으로 만들 필요없이 uicontrol 콜백 함수로 작동 할 수있게하는 방법이 있습니까? 개념적으로이 메서드는 클래스 사용자가 결코 호출하면 안되기 때문에 public이 아니어야합니다. 콜백을 트리거하는 UI 이벤트의 결과로 호출되어야합니다. 그러나 메서드의 액세스를 private 또는 protected로 설정하면 콜백이 작동하지 않습니다. 내 클래스는 hgsetget에서 파생되며 2008a 클래스 정의 구문을 사용하여 정의됩니다. 개인 속성이 문제를 해결하기 위해 보인다 콜백의 기능 핸들을 저장MATLAB에서 클래스 메서드는 공개되지 않고 uicontrol 콜백으로 작동 할 수 있습니까?


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 

답변

8

: 같은


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 

콜백 코드는 같습니다

uicontrol 코드가 같이 보입니다. 사용해보기 :

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