2016-12-27 1 views
0

struct 유형의 클래스 특성에 함수를 첨부 할 수 있습니까? 의도 된 사용 :Matlab 구조체에 함수 부착하기

% Definition: 
classdef a < handle 
    properties 
    bar 
    end 
    methods 
    function obj = a() 
     obj.bar = struct; 
     %obj.bar.attachFunction('apply', @someFunction); <-- something like this 
    end 
    end 
end 

% Usage: 
foo = a(); 
foo.bar.apply('test'); 
foo.bar.var1 = 1; 
foo.bar.var2 = 2; 

답변

0

오, 그건 사실 아주 간단하게 내 마음을 사용했습니다.

classdef a < handle 
    properties 
    bar 
    end 
    methods 
    function obj = a() 
     obj.bar = struct; 
     obj.bar.apply = @(str) @obj.barApply(str); 
    end 
    end 
    methods (Access=protected) 
    function barApply(obj, str) 
     obj.bar.something = str; 
    end 
    end 
end 

foo = a(); 
foo.bar.apply('monkey'); 
foo.bar.apple = 2; 
+0

여기서 개체의 속성에 필드를 추가하고 있지만 인스턴스에 동적 속성을 추가하는 것이 좋습니다. 여기에 대해서는 https://www.mathworks.com/help/matlab/matlab_oop/dynamic-properties--adding-properties-to-an-instance.html에서 읽을 수 있습니다. –