将函数附加到Matlab结构

时间:2016-12-27 09:00:18

标签: matlab matlab-struct

是否可以将函数附加到struct类型的类属性?预期用途:

% 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;

1 个答案:

答案 0 :(得分: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;