Treating input arguments (not paying attention to order of number)

There is a "master class" that instanciates different objects depending on the users choice. A stuct array with a set of parameter should be the indicator if an object is created or not. For every init-function there is one input struct.
First attempt was..
% Constructor
function obj = MasterClass(object1_struct,object2_struct,object3_struct,object4_struct)
if exist('object1_struct','var')
obj.initObj1(object1_struct);
end
if exist('object2_struct','var')
obj.initObj2(object2_struct);
end
if exist('object3_struct','var')
obj.initObj3(object3_struct);
end
if exist('object4_struct','var')
obj.initObj4(object4_struct);
end
end
but of cause, if I call e.g.
obj = MasterClass(object3_struct,object2_struct)
Object1 and Object2 are created with inputs object3_struct and object2_struct. So what's the way to do it, without insert empty structs or pay attention to the order?

 Réponse acceptée

Walter Roberson
Walter Roberson le 13 Déc 2019
Use name/value pairs, unless there is something inside of the structs that identifies which kind of object it is for, or some other way of deducing from the struct what its purpose is.

5 commentaires

Thanks, good idea. But could you help me handaling the var input arguments? The struct names already contains the required info for creation.
But don't know how to compare struct names and pass the struct through. Kind of...?
function obj = MasterClass(varargin)
if nargin < 1
fprintf('No Infos.\n')
return
end
for i = 1:length(varargin)
if strcmp(inputname(i),'object1_struct')
obj.initObj1(% How to pass in the i input struct???);
elseif strcmp(inputname(i),'object2_struct')
obj.initObj2(% How to pass in the i input struct???);
elseif strcmp(inputname(i),'object3_struct')
obj.initObj3(% How to pass in the i input struct???);
elseif strcmp(inputname(i),'object4_struct')
obj.nitObj4(% How to pass in the i input struct???);
end
end
end
varargin{i}
I really do not recommend this approach. If you are writing a class for people to use you should not be forcing them to use specific variable names.
"The struct names already contains the required info for creation."
This is bad data design.
Forcing meta-data into variable names makes code slow, complex, obfuscated, buggy, and harder to debug. Meta-data is data, and data should be stored in a variable, not in the variable name. Your code would be a lot simpler, more efficient, and easier to work with if you did not force meta-data into variable names.
I see. For me it's just the first step to initiate a certain setup. I Mean I have different (changing) types of equitment I want to communicate with, e.g, an AD-Converter, a powersupply, electronic load... each one has its own class I'm wrapping together.
I use structs to concentrate inital properties like COM port number, sampling rate etc. The struct names like 'AD', 'Source', 'Load' don't change, just the properties. Is it better to put a name as a string into the structs and compare them in the constructor, like AD.name = 'AD'?
Perhaps it would make sense to pass in an array of struct, each member of which identified its purpose?

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by