- Use nargin and some switch or if statements to detemrine the default parameters.
- Use varargin and logical indexing to detect non-specified arguments (e.g. empty).
- Use key-value pairs (usually with varargin) and strcmpi to detect the keys.
- Use a scalar structure to hold all options.
How to support default parameter in MATLAB FUNCTION ?
2,701 views (last 30 days)
Show older comments
M.K.H. CHANDAN
on 18 May 2015
Edited: Stefan Schuberth
on 23 Jan 2023
1 . As I know , there is no default parameter support on MATLAB FUNCTION like other high level programming language c,c++, Is there any other methodology through which it can be done like other compiler C,C++? or programmer has to do using his own logic through varargin.
0 Comments
Accepted Answer
Stephen23
on 18 May 2015
Edited: Stephen23
on 18 May 2015
MATLAB is an interpreted language, not a compiled language, so there is no compiler as such.
In any case there is no inbuilt syntax to allow such things as:
function out = fun(val_1=def_val, val_2=def_val2, ...)
The most common ways of dealing with this are:
Using varargin with nargin or logical indexing is very easy:
function foobar(varargin)
Defaults = {A,B,C...};
Defaults(1:nargin) = varargin;
OR
function foobar(varargin)
Defaults = {A,B,C...};
idx = ~cellfun('isempty',varargin);
Defaults(idx) = varargin(idx);
MATLAB themselves use different methods in different functions. Have a look at the ODE functions to see the structure syntax, many plotting functions use the key-value syntax.
You can easily combine key-value and scalar-structure in one function, see my FEX submission here to see how:
More Answers (3)
Jan Siegmund
on 4 Jul 2020
Edited: Jan Siegmund
on 8 Jul 2020
The Answers are not up to date. Modern MATLAB supports the arguments Block:
function out = foo(in)
arguments
in(1,1) double {mustBeFinite} = 0;
end
out = in + 1;
end
For more Info check this Page: https://de.mathworks.com/help/matlab/matlab_prog/function-argument-validation-1.html
Be careful with the validation functions though. They are not "is"-functions returning logical, rather they throw an error. Here is a list of predefined ones: https://de.mathworks.com/help/matlab/matlab_prog/argument-validation-functions.html
If the default argument is a class you may want to use <Class>.empty:
arguments
nameValueArgs.BaseFigure matlab.ui.Figure = matlab.ui.Figure.empty;
end
2 Comments
Ingrid
on 18 May 2015
in your function you need to check how many parameters have been passed
function myFunction(variable1,variable2, optionalVarChangeDefaultParam)
if nargin > 2
defaultParam = optionalVarChangeDefaultParam;
else
defaultParam = 2;
end
Stefan Schuberth
on 23 Jan 2023
Edited: Stefan Schuberth
on 23 Jan 2023
function testFun(a,b)
arguments a=10; end % default value
arguments b(3,3) double {mustBePositive} = 10; end
% arguments a(size1dim,size2dim) varType {validationFunction} = defaultValue; end
disp(a);
end
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!