cell Array of objects to object method

10 次查看(过去 30 天)
Hi all, I'm new to OOP concepts and I'm trying to port an old code that used to make massive use of structs to a class-based architecture.
I have a class named "Component" with a method called "Cascade. This are the input parser lines of the Cascade method
function obj = Cascade(varargin)
switch nargin
case 1
if iscell(varargin{1})&&length(varargin{1})>1
nDev = length(varargin{1});
obj = Cascade(varargin{1},varargin{2});
for iD = 2:(nDev-1)
obj = Cascade(obj,varargin{iD+1});
end
return
else
error('one input argument is allowed for at least 1x2 cell arrays of devices');
end
case 2
if (varargin{1}.Out.nPorts==varargin{2}.In.nPorts)
Cmatrix = (1:varargin{1}.Out.nPorts)'*[1,1];
else
error('number of ports in cascaded devices do not correspond, provide a valid connection matrix');
end
case 3
Cmatrix = varargin{3};
case 4
error('Unsupported number of inputs');
end
CMP1 = varargin{1};
CMP2 = varargin{2};
I basically want to operate on two "components" and iteratively call the method if I pass a bigger collection.
Is there a way to make this method work if I call it like
CMP = Cascade({CMP1,CMP2,CMP3});
instead of throwing the error
Undefined function 'Cascade' for input arguments of type 'cell'?
Of course I could redefine "Cascade" as to be able to do something like
CMP = Cascade(CMP1, {CMP2,CMP3});
And this will obviously work, but it makes less logical sense to me. Another way would be of defining an external version of Cascade which just parse the inputs but I don't like this solution either since I want everything contained in the class definition.
  6 个评论
Adam
Adam 2019-2-14
An array of objects would make the most sense, but if you don't want that then you have to just either a syntax that passes in 1 followed by a cell array of others (which seems very odd as a design) or create a static function where you handle your cell array. Since you are returning something called obj the static function approach seems next best.
Walter Roberson
Walter Roberson 2019-2-14
Perhaps permit two different syntaxes: a cell array second parameter, or alternately a variable number of arguments each of which is a Component. Then
Cascade(01, {o2, ...oN})
and
Cascade(O1, o2, o3, ... oN)
would both work.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by