Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 422
hj-007 Avatar asked Sep 20 '13 13:09

hj-007


People also ask

Can you set default values to Python arguments?

Python allows function arguments to have default values.

What is an input argument in MATLAB?

Support Variable Number of Inputs Define a function that accepts a variable number of input arguments using varargin . The varargin argument is a cell array that contains the function inputs, where each input is in its own cell.

Can a function argument have default value?

Once a default value is used for an argument in the function definition, all subsequent arguments to it must have a default value as well. It can also be stated that the default arguments are assigned from right to left.

What is the argument function in MATLAB?

Introduction to Argument Validation Function argument validation is declarative, which enables MATLAB® desktop tools to extract information about a function by inspection of specific code blocks.


1 Answers

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.

like image 183
Mohsen Nosratinia Avatar answered Sep 22 '22 12:09

Mohsen Nosratinia