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)
else:
return h(x,y)
into Matlab
function rtrn = f(x,y)
if y == []:
rtrn = g(x);
else
rtrn = h(x,y);
end;
end
As you can see currently I am using []
as None
. Is there a better way to do this?
NaN
虽然不相同,但通常起到类似的作用。
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;
end
Given a single input argument, this function squares the input value. Given two inputs, it adds them together.
nargin
is definitely the easiest way of doing it. Also it is usually good practice to validate the number of input argument using nargchk :
function e = testFunc(a,b,c,d)
error( nargchk(2, 4, nargin, 'struct') );
% set default values
if nargin<4, d = 0; end
if nargin<3, c = 0; end
% ..
c = a*b + c*d;
end
... which acts as a way to ensure the correct number of arguments is passed. In this case, a minimum of two arguments are required, with a maximum of four .
If nargchk
detects no error, execution resumes normally, otherwise an error is generated. For example, calling testFunc(1)
generates:
Not enough input arguments.
UPDATE: A new function was introduced in R2011b
narginchk
, which replaces the use of the deprecatednargchk
+error
seen above:narginchk(2,4);
You can use functions like: exist and isempty to check whether a variable exists and whether it is empty respectively:
if ~exist('c','var') || isempty(c)
c = 10;
end
which allows you to call your function such as: testFunc(1,2,[],4)
telling it to use the default value for c
but still giving a value for d
You could also use varargin to accept a variable number of arguments.
Finally a powerful way to parse and validate named inputs is to use inputParser
To see examples and other alternatives of passing arguments and setting default values, check out this post and its comments as well.
The equivalent to Python None
in MATLAB is string(missing)
To test, type the following in your command window: py.type( string(missing) )
It returns <class 'NoneType'>
MATLAB
to python
data types documentation here
If you want to pass None
into a Python
function that you are calling from MATLAB
, then you would pass in string(missing)
. This argument would show up as None
in the Python
function, for example, if you are detecting for None
such as if arg1 == None
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.