The Matlab equivalent of Python's "None"


Is there a keyword in Matlab that is roughly equivalent to None in python?

I am trying to use it to mark an optional argument to a function. I am translating the following Python code

def f(x,y=None):
    if y == None:
        return g(x)
        return h(x,y)

into Matlab

function rtrn = f(x,y)
    if y == []:
        rtrn = g(x);
        rtrn = h(x,y);

As you can see currently I am using [] as None. Is there a better way to do this?

11/15/2009 1:35:27 PM

Accepted Answer

in your specific case. you may use nargin to determine how many input arguments here provided when calling the function.

from the MATLAB documentation:

The nargin and nargout functions enable you to determine how many input and output arguments a function is called with. You can then use conditional statements to perform different tasks depending on the number of arguments. For example,

function c = testarg1(a, b) 
     if (nargin == 1)
         c = a .^ 2; 
     elseif (nargin == 2)
         c = a + b; 

Given a single input argument, this function squares the input value. Given two inputs, it adds them together.

11/15/2009 1:46:38 PM

NaN while not equivalent, often serves the similar purpose.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow