Treating input arguments (not paying attention to order of number)
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Konrad Warner
am 13 Dez. 2019
Kommentiert: Walter Roberson
am 14 Dez. 2019
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?
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 13 Dez. 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 Kommentare
Walter Roberson
am 14 Dez. 2019
Perhaps it would make sense to pass in an array of struct, each member of which identified its purpose?
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!