hj-007
hj-007

Reputation: 342

Does MATLAB lets you assign default value for input arguments for a function like python does?

I am working on a project and have many functions to create and they do need lots of debugging so instead of just hitting the run button i have to go to command window and give a function call.

does MATLAB support assignment of default values to input arguments like python does?

In python

def some_fcn(arg1 = a, arg2 = b)
% THE CODE

if you now call it without passing the arguments it doesn't give errors but if you try the same in MATLAB it gives an error.

Upvotes: 8

Views: 11869

Answers (5)

benJephunneh
benJephunneh

Reputation: 736

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.

Upvotes: 1

Bas Swinckels
Bas Swinckels

Reputation: 18488

If you are writing a complex function that requires validation of inputs, default argument values, key-value pairs, passing options as structs etc., you could use the inputParser object. This solution is probably overkill for simple functions, but you might keep it in mind for your monster-function that solves equations, plots results and brings you coffee. It resembles a bit the things you can do with python's argparse module.

You configure an inputParser like so:

>> p = inputParser();
>> p.addRequired('x', @isfinite)       % validation function
>> p.addOptional('y', 123)             % default value
>> p.addParamValue('label', 'default') % default value

Inside a function, you would typically call it with p.parse(varargin{:}) and look for your parameters in p.Results. Some quick demonstration on the command line:

>> p.parse(44); disp(p.Results)
    label: 'default'
        x: 44
        y: 123
>> p.parse()
Not enough input arguments.
>> p.parse(Inf)
Argument 'x' failed validation isfinite.
>> p.parse(44, 55); disp(p.Results)
    label: 'default'
        x: 44
        y: 55
>> p.parse(13, 'label', 'hello'); disp(p.Results)
    label: 'hello'
        x: 13
        y: 123
>> p.parse(88, 13, 'option', 12)
Argument 'option' did not match any valid parameter of the parser.

Upvotes: 7

Mohsen Nosratinia
Mohsen Nosratinia

Reputation: 9864

For assigning default values, one might find it easier to manage if you use exist function instead of nargin.

function f(arg1, arg2, arg3)
if ~exist('arg2', 'var')
    arg2 = arg2Default;
end

The advantage is that if you change the order of arguments, you don't need to update this part of the code, but when you use nargin you have to start counting and updating numbers.

Upvotes: 13

am304
am304

Reputation: 13886

You can use nargin in your function code to detect when no arguments are passed, and assign default values or do whatever you want in that case.

Upvotes: 0

Dan
Dan

Reputation: 45752

You can kind of do this with nargin

function out = some_fcn(arg1, arg2)
    switch nargin
       case 0
             arg1 = a;
             arg2 = b;
    %//etc
end

but where are a and b coming from? Are they dynamically assigned? Because that effects the validity of this solution

After a few seconds of googling I found that as is often the case, Loren Shure has already solved this problem for us. In this article she outlines exactly my method above, why it is ugly and bad and how to do better.

Upvotes: 5

Related Questions