将函数附加到Matlab结构

将函数附加到Matlab结构,matlab,matlab-struct,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

是否可以将函数附加到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;
%定义:
classdef a<手柄
性质
酒吧
结束
方法
函数obj=a()
obj.bar=结构;

%obj.bar.attachFunction('apply',@someFunction) 哦,那其实很简单,我用过脑子

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;
classdef a
在这里,您将向对象的属性添加字段,但我认为您可能还对向实例添加动态属性感兴趣。您可以在此处阅读: