cell Array of objects to object method

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 Commenti

you need to decide whether you are using a static method or a constructor, or if you want to permit non-cell arrays of Component objects .
No, you cannot extend cell class to add a method named Cascade to it in the case that the cell elements all happen to contain Component objects .
You should use an object array instead, it is far better than a cell array:
CMP = Cascade( [CMP1, CMP2, CMP3] )
Object arrays allow you to, for example, return all property X values from each class, or call functions on all elements of the object.
However, the latter only works if your function implementation supports object arrays. By default most implementations you would do don't support this so using a class in this way can be a bit hit and miss with some functions allowing you to pass an array of objects to act upon and others not supporting this.
You can then remove all the iscell stuff and simple test isscalar or numel( obj ), for example to know if you are dealing with a scalar object or an array of them. You can loop over the array just as you would any other array to act on the individual objects, though where you can acting on all at the same time in vectorised fashion is generally faster, just as with numeric arrays. This is not always possible though, and even when it is, can lead to some ugly syntax that often isn't worth the penalty to readability for a miniscule time saving.
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.
Why prefer that, if it doesn't make sense for the way you use Cascade?
Francesco Piccioli
Francesco Piccioli il 14 Feb 2019
Modificato: Francesco Piccioli il 14 Feb 2019
Because Cascade can only operate on "Components". It doesn't make sense to have a general version of it.
Probably I should just accept that the right syntax is Cascade(o1,{o2...oN}) since I only Cascade one object with another object (which might be as well a cascade...). It just seemed more right to me the {o1..oN} flavour but it doesn't make much sense to how cell arrays are used within matlab. To reply to Adam and Walter no, I don't want to permit arrays of "Components".
Again, Since I'm new to OOP concepts the way I'm thinking about the architecture might very well be wrong, Thank you for your suggestions and comments!
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.
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.

Accedi per commentare.

Risposte (0)

Categorie

Prodotti

Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by