Clear Filters
Clear Filters

Default dimension of one-dimensional array of objects

7 views (last 30 days)
Is there a way to specify default dimensions of an object array when only one dimension is used on instantiation. For example, initialising array of objects like below will return an array of dimensions (1, 5)
objArray(5) = MyClass;
I would like the default behaviour to be equivalent to the one below, where the array is of dimensions (5, 1).
objArray(5, 1) = MyClass;
I would require this to be able to extract certain properties of the class in correct arrangement after concatenation, e.g.
[objArray.propertyOne]

Accepted Answer

Matt J
Matt J on 12 Jul 2021
Edited: Matt J on 12 Jul 2021
The result of [objArray.propertyOne] will always be a row vector independent of the shape of objArray, so I don't think having the output object array in column vector form will help you (but you can use vertcat(objArray.propertyOne) instead ).
To answer your question, though, you can ensure a column vector shape by building and shaping the array inside the constructor, e.g., with
function objArray=MyClass(varargin)
% objArray=MyClass(dim1,dim2,dim3,...)
if nargin==0, return;end
objArray(varargin{:})=MyClass;
if nargin==1
objArray=objArray(:);
end
or whatever the condition should be.

More Answers (0)

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!