필터 지우기
필터 지우기

Default dimension of one-dimensional array of objects

조회 수: 7 (최근 30일)
Leon
Leon 2021년 7월 12일
편집: Matt J 2021년 7월 12일
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]

채택된 답변

Matt J
Matt J 2021년 7월 12일
편집: Matt J 2021년 7월 12일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by