how to not use all input arguments in the function because some of the arguments are fixed?
3 views (last 30 days)
Show older comments
Alfandias Saurena
on 16 Feb 2022
Commented: Alfandias Saurena
on 16 Feb 2022
how to not use all input arguments in the function because some of the arguments are fixed?
Accepted Answer
DGM
on 16 Feb 2022
Edited: DGM
on 16 Feb 2022
If you're writing a function and want certain arguments to be optional (with internal defaults), read about varargin
From the scope of the function, varargin can be handled as a cell array. How you parse/validate its contents is up to your needs.
I generally assign all the parameters to their default values prior to parsing the inputs, overwriting the defaults as the user-defined values are collected from varargin.
3 Comments
More Answers (1)
Steven Lord
on 16 Feb 2022
Edited: Steven Lord
on 16 Feb 2022
You can use an anonymous function "adapter".
f = @(in1, in2) max(in1, in2); % I could have used @max
% but I wanted to be explicit
f_2p5 = @(x) f(x, 2.5); % Call f with the first input specified by
% the user and the second fixed by me as 2.5
f_2p5(1:5)
f(1:5, 2.5)
0 Comments
See Also
Categories
Find more on Argument Definitions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!