1

In MATLAB's new object model (classdef, etc): If I have an array of the object, and I call an ordinary method, is the methods called for each object, or for the entire array, i.e. is a single object passed into the method, or the entire array? I know that in the old model, it got dispatched as the entire array.

chappjc
  • 30,359
  • 6
  • 75
  • 132
Marc
  • 3,259
  • 4
  • 30
  • 41

2 Answers2

6

If you have:

classdef MyObject

methods
    function foo(obj)
    ...
end

And you then call

>> foo(myObjArray)

Then the single call to foo() will receive the entire array. From there you can write code to handle a scalar case of obj or vector case of obj.

Andrew
  • 1,167
  • 7
  • 5
-1

It depends on if your method is vectorized. For a trivial example:

Not vectorized

function result = mySimpleMultiply(a,b)

result = a*b;

Vectorized

function result = myVectorizedMultiply(a,b)

result = a.*b;
Scottie T
  • 11,729
  • 10
  • 45
  • 59
  • I sure you were trying to help, but how does this answer the question at all? – Marc Apr 03 '09 at 19:20
  • Maybe I should have added that MATLAB treats all variables like arrays, so if you have a single element, say x = 2, x is a 1-by-1 double array. You then have to write your functions, OO or not, accordingly. – Scottie T Apr 05 '09 at 13:14