How can I declare function in MATLAB with optional arguments?
For example: function [a] = train(x, y, opt), where opt must be an optional argument.
How can I declare function in MATLAB with optional arguments?
For example: function [a] = train(x, y, opt), where opt must be an optional argument.
Mathworks:Function Argument Validation
There is a detailed description about how to user optional parameters in function. Also, you can learn how to use function like build-in matlab fun like
fun(arg1,'key1',value1)
There is some example using the trick from above:
function [a] = train(x,y,opt)
arguments
x double
y double
opt {mustBeNonempty} = true
% this set the default value for opt is true
end
if opt
a = 1;
else
a = x+y;
end
end