Creating Object Arrays

Building Arrays in the Constructor

A constructor function can return an object array by making the necessary assignments when initializing the output argument. For example, the following function creates an m-by-n object array and initializes the Value property of each object to the corresponding element in the input argument f:

function obj = obj_class(f)
   m = size(f,1);
   n = size(f,2);
   for i = 1:m
      for j = 1:n
         obj(i,j).Value = f(i,j);
      end
   end
end

Initial Values of Object Arrays

When creating an object array, all property values are assigned whatever values are specified in the property definitions, or default to the value of empty double (i.e., []).

Creating Empty Arrays

All classes have a static method named empty that creates an empty array belonging to the class. Empty arrays have no instance data. This method enables you to specify the dimensions of the output array, but at least one of the dimensions must be 0. For example:

emptyArray = myClass.empty(5,0);

creates a 5–by-0 empty array of myClass objects. Calling empty with no arguments returns a 0–by-0 empty array.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS