Default dimension of one-dimensional array of objects

3 visualizaciones (últimos 30 días)
Leon
Leon el 12 de Jul. de 2021
Editada: Matt J el 12 de Jul. de 2021
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]

Respuesta aceptada

Matt J
Matt J el 12 de Jul. de 2021
Editada: Matt J el 12 de Jul. de 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.

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by