2016-12-27 7 views
0

構造体のクラスプロパティに関数を追加することはできますか?目的の使用法: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 –

関連する問題