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

4 ビュー (過去 30 日間)
Konrad Warner
Konrad Warner 2019 年 12 月 13 日
コメント済み: Walter Roberson 2019 年 12 月 14 日
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?

採用された回答

Walter Roberson
Walter Roberson 2019 年 12 月 13 日
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 件のコメント
Konrad Warner
Konrad Warner 2019 年 12 月 14 日
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'?
Walter Roberson
Walter Roberson 2019 年 12 月 14 日
Perhaps it would make sense to pass in an array of struct, each member of which identified its purpose?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by