Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab Arrayfun with Handle Class

Tags:

matlab

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?

like image 363
krapht Avatar asked Jan 31 '26 20:01

krapht


2 Answers

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]);
like image 88
Andrey Rubshtein Avatar answered Feb 03 '26 11:02

Andrey Rubshtein


Yes, you can:

arrayfun(@(x,y)x.SetProperty(y), yourHandleObjects, theValues)
like image 30
Jonas Avatar answered Feb 03 '26 10:02

Jonas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!