How to pass extra parameters to custom Output Function?

2 ビュー (過去 30 日間)
e_frog
e_frog 2022 年 2 月 18 日
コメント済み: e_frog 2022 年 2 月 18 日
Hi,
I want to pass
test_var = 5;
to the custom output function
[state, options,optchanged] = testFCN(options,state,flag)
in the optimoptions of gamultiobj().
I tried:
[state, options,optchanged] = testFCN(options,state,flag,settings)
and
test_handle1 = @(options,state,flag) testFCN(options,state,flag,test_var);
test_handle2 = @(x) testFCN(x,test_var);
but I always get the error
Too many input arguments.
What am I doing wrong?

採用された回答

Voss
Voss 2022 年 2 月 18 日
Regardless of whether you define a function handle that uses testFCN or you call testFCN directly, make sure your testFCN is defined to accept up to four input arguments.
(You can check how many arguments are given using nargin, and act accordingly.)
test_var = 5;
options = [];
state = [];
flag = false;
settings = [];
% testFCN_1 is defined as having 3 input arguments
% testFCN_2 is defined as having 4 input arguments
% testFCN is defined as having 4 input arguments, with the 4th being optional
% (see the definitions at the bottom)
% try calling testFCN_1 with 3 inputs -> OK
try
[state, options,optchanged] = testFCN_1(options,state,flag);
disp('Worked OK');
catch ME
disp(ME.message);
end
Worked OK
% try calling testFCN_1 with 4 inputs -> error
try
[state, options,optchanged] = testFCN_1(options,state,flag,settings);
disp('Worked OK');
catch ME
disp(ME.message);
end
Too many input arguments.
% try calling testFCN_2 with 3 inputs -> different error (if 4th input is needed)
try
[state, options,optchanged] = testFCN_2(options,state,flag);
disp('Worked OK');
catch ME
disp(ME.message);
end
Not enough input arguments.
% try calling testFCN_2 with 4 inputs -> OK
try
[state, options,optchanged] = testFCN_2(options,state,flag,settings);
disp('Worked OK');
catch ME
disp(ME.message);
end
Worked OK
% testFCN (using nargin) can take 3 or 4 inputs:
% try calling testFCN with 3 inputs -> OK
try
[state, options,optchanged] = testFCN(options,state,flag);
disp('Worked OK');
catch ME
disp(ME.message);
end
Worked OK
% try calling testFCN with 4 inputs -> OK
try
[state, options,optchanged] = testFCN(options,state,flag,settings);
disp('Worked OK');
catch ME
disp(ME.message);
end
Worked OK
function [state, options,optchanged] = testFCN_1(options,state,flag) % 3 input arguments
optchanged = flag;
end
function [state, options,optchanged] = testFCN_2(options,state,flag,settings) % 4 input arguments
optchanged = settings;
end
function [state, options,optchanged] = testFCN(options,state,flag,settings) % 4 input arguments, 4th optional
if nargin < 4
% if settings is not given, use some default value
settings = [];
end
optchanged = settings;
end
  1 件のコメント
e_frog
e_frog 2022 年 2 月 18 日
Thanks, I copied my mistake to this thread. 'settings' should have been 'test_var' as well. Now it works!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by