简体   繁体   English

带有句柄类的Matlab Arrayfun

[英]Matlab Arrayfun with Handle Class

Say I have a 1x2 object array of a handle class with a method SetProperty. 假设我有一个带有方法SetProperty的句柄类的1x2对象数组。 Can I use arrayfun to call the SetProperty method for each class, along with a vector for it to use to set the property value? 我可以使用arrayfun为每个类调用SetProperty方法,以及用于设置属性值的向量吗?

You can also design the class so that the call to SetProperty will be vectorized: 您还可以设计类,以便对SetProperty的调用进行矢量化:

 class Foo < handle
      methods(Access=public)
            function SetProperty(this,val)
                 assert(numel(this)==numel(val));
                 for i=1:numel(this)
                      this(i).prop = val(i);
                 end
            end
      end
end

Then, you can create a vector and call the method on it directly: 然后,您可以创建一个向量并直接在其上调用该方法:

    f = repmat(Foo(),[1 2]);
    f.SetProperty( [5 3]);

是的你可以:

arrayfun(@(x,y)x.SetProperty(y), yourHandleObjects, theValues)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM