Azzera filtri
Azzera filtri

Default dimension of one-dimensional array of objects

7 visualizzazioni (ultimi 30 giorni)
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]

Risposta accettata

Matt J
Matt J il 12 Lug 2021
Modificato: Matt J il 12 Lug 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.

Più risposte (0)

Categorie

Scopri di più su Construct and Work with Object Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by