call function with multiparameter:what is the best solution?

%WHAT IS best solution ?
%Solution A:
a)CaricoSistemi2Last_Struct(Settings,[])
b)CaricoSistemi2Last_Struct(Settings,mat)
function D_=CaricoSistemi2Last_Struct(Settings,mat)
if isempty(mat)
D1=load(Settings.tslist,'-mat');
D_=D1.StratList;
else
D_=mat;
end
....
....
end
%Solution B:
a)CaricoSistemi2Last_Struct(Settings)
b)CaricoSistemi2Last_Struct(Settings,mat)
function D_=CaricoSistemi2Last_Struct(Settings,mat)
if nargin<2
D1=load(Settings.tslist,'-mat');
D_=D1.StratList;
else
D_=mat;
end
....
....
end

1 件のコメント

Les Beckham
Les Beckham 2023 年 11 月 13 日
I would say that this is mostly a matter of personal preference.
Solution B is probably more commonly used. Solution A is often used if you have additional arguments after your "optional" second argument.

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

 採用された回答

Chunru
Chunru 2023 年 11 月 14 日

0 投票

You could also use arguments block. doc arguments for more details.
a)CaricoSistemi2Last_Struct(Settings)
b)CaricoSistemi2Last_Struct(Settings,mat)
function D_=CaricoSistemi2Last_Struct(Settings,mat)
arguments
Settings
mat = []; % default
end
if isempty(mat)
D1=load(Settings.tslist,'-mat');
D_=D1.StratList;
end
% More codes

3 件のコメント

shamal
shamal 2023 年 11 月 14 日
if i want to call using second parameter CaricoSistemi2Last_Struct(mat)
like the reserved word "arguments" it understands that the name passed feeds the second parameter and not the first?
Chunru
Chunru 2023 年 11 月 14 日
編集済み: Chunru 2023 年 11 月 14 日
Matlab function arguments are positional, meaning that the positions are important.
For example, you can not call using second parameter CaricoSistemi2Last_Struct(mat) in the following code
function D_=CaricoSistemi2Last_Struct(Settings,mat)
arguments
Settings
mat = []; % default
end
If you do want to use the name-value-pair way of passing argument, you can do the following
function D_=CaricoSistemi2Last_Struct(options)
arguments
options.Settings = [] % your default
options.mat = []; % default
end
Then you can call the function CaricoSistemi2Last_Struct(mat=matval)
Again, doc arguments for more details.
shamal
shamal 2023 年 11 月 14 日
ok thanks

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeModeling についてさらに検索

質問済み:

2023 年 11 月 13 日

コメント済み:

2023 年 11 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by