필터 지우기
필터 지우기

How to pass extra parameters to custom Output Function?

조회 수: 3 (최근 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 CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by