Say I have a 1x2 object array of a handle class with a method SetProperty. 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?
You can also design the class so that the call to SetProperty will be vectorized:
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]);
Yes, you can:
arrayfun(@(x,y)x.SetProperty(y), yourHandleObjects, theValues)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With