MathWorks has a new solution for this in R2019b, namely, the arguments
block. There are a few rules for the arguments block, naturally, so I would encourage you to learn more by viewing the Function Argument Validation help page. Here is a quick example:
function ret = someFunction( x, y )
%SOMEFUNCTION Calculates some stuff.
arguments
x (1, :) double {mustBePositive}
y (2, 3) logical = true(2, 3)
end
% ...stuff is done, ret is defined, etc.
end
Wrapped into this is narginchk
, inputParser
, validateattributes
, varargin
, etc. It can be very convenient. Regarding default values, they are very simply defined as those arguments that equal something. In the example above, x
isn't given an assignment, whereas y = true(2, 3)
if no value is given when the function is called. If you wanted x
to also have a default value, you could change it to, say, x (1, :) double {mustBePositive} = 0.5 * ones(1, 4)
.
There is a more in-depth answer at How to deal with name/value pairs of function arguments in MATLAB
that hopefully can spare you some headache in getting acquainted with the new functionality.