Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set default values for functions parameters in MATLAB?

Is it possible to have default arguments in MATLAB?

For instance, here:

function wave(a, b, n, k, T, f, flag, fTrue=inline('0'))

I would like to have the true solution be an optional argument to the wave function. If it is possible, what is the proper way to do this?

Currently, I am trying what I posted above and I get:

??? Error: File: wave.m Line: 1 Column: 37
The expression to the left of the equals sign is not a valid target for an assignment.
like image 323
Scott Avatar asked Sep 25 '22 05:09

Scott


People also ask

Can you assign the default values to a function parameters?

Default parameter in JavascriptThe default parameter is a way to set default values for function parameters a value is no passed in (ie. it is undefined ). In a function, Ii a parameter is not provided, then its value becomes undefined . In this case, the default value that we specify is applied by the compiler.

Can all the parameters of a function can be default parameters?

C. All the parameters of a function can be default parameters.

Can you assign the default values to a function parameters True or false?

It is called default function parameters . It allows formal parameters to be initialized with default values if no value or undefined is passed.

What rules apply for functions with default parameters?

Rule 1: creating functions. When programmers give a parameter a default value, they must give default values to all the parameters to right of it in the parameter list.


3 Answers

There isn't a direct way to do this like you've attempted.

The usual approach is to use "varargs" and check against the number of arguments. Something like:

function f(arg1, arg2, arg3)

  if nargin < 3
    arg3 =   'some default'
  end

end

There are a few fancier things you can do with isempty, etc., and you might want to look at MATLAB central for some packages that bundle these sorts of things.

You might have a look at varargin, nargchk, etc. They're useful functions for this sort of thing. varargs allow you to leave a variable number of final arguments, but this doesn't get you around the problem of default values for some/all of them.

like image 157
simon Avatar answered Oct 17 '22 12:10

simon


I've used the inputParser object to deal with setting default options. MATLAB won't accept the Python-like format you specified in the question, but you should be able to call the function like this:

wave(a, b, n, k, T, f, flag, 'fTrue', inline('0'))

After you define the wave function like this:

function wave(a, b, n, k, T, f, flag, varargin)

    i_p = inputParser;
    i_p.FunctionName = 'WAVE';

    i_p.addRequired('a', @isnumeric);
    i_p.addRequired('b', @isnumeric);
    i_p.addRequired('n', @isnumeric);
    i_p.addRequired('k', @isnumeric);
    i_p.addRequired('T', @isnumeric);
    i_p.addRequired('f', @isnumeric);
    i_p.addRequired('flag', @isnumeric);
    i_p.addOptional('ftrue', inline('0'), 1);

    i_p.parse(a, b, n, k, T, f, flag, varargin{:});

Now the values passed into the function are available through i_p.Results. Also, I wasn't sure how to validate that the parameter passed in for ftrue was actually an inline function, so I left the validator blank.

like image 58
Matt Avatar answered Oct 17 '22 12:10

Matt


Another slightly less hacky way is

function output = fun(input)
   if ~exist('input','var'), input='BlahBlahBlah'; end
   ...
end
like image 22
Peter Avatar answered Oct 17 '22 10:10

Peter