简体   繁体   English

Matlab 相当于 Python 的“None”

[英]The Matlab equivalent of Python's "None"

Is there a keyword in Matlab that is roughly equivalent to None in python? Matlab中是否有关键字大致相当于python中的None

I am trying to use it to mark an optional argument to a function. I am translating the following Python code我正在尝试使用它来标记 function 的可选参数。我正在翻译以下 Python 代码

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

into Matlab进入 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 .如您所见,我目前正在使用[]作为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. 您可以使用nargin来确定调用函数时提供的输入参数数量。

from the MATLAB documentation : 来自MATLAB文档

The nargin and nargout functions enable you to determine how many input and output arguments a function is called with. nargin和nargout函数使您能够确定调用函数的输入和输出参数的数量。 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. nargin绝对是最简单的方法。 Also it is usually good practice to validate the number of input argument using nargchk : 使用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. 如果nargchk检测到错误,则执行正常恢复,否则会生成错误。 For example, calling testFunc(1) generates: 例如,调用testFunc(1)生成:

Not enough input arguments.

UPDATE: A new function was introduced in R2011b narginchk , which replaces the use of the deprecated nargchk + error seen above: 更新:在R2011b narginchk中引入了一个新函数,它取代了上面看到的弃用的nargchk + error的使用:

 narginchk(2,4); 

You can use functions like: exist and isempty to check whether a variable exists and whether it is empty respectively: 您可以使用功能,如: 存在的isEmpty检查变量是否存在以及是否分别是空的:

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 它允许你调用你的函数,例如: testFunc(1,2,[],4)告诉它使用c的默认值,但仍然给出d的值

You could also use varargin to accept a variable number of arguments. 您还可以使用varargin接受可变数量的参数。

Finally a powerful way to parse and validate named inputs is to use inputParser 最后,解析和验证命名输入的一种强大方法是使用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)相当于 Python None in MATLAB 是string(missing)

To test, type the following in your command window: py.type( string(missing) )要进行测试,请在命令 window 中键入以下内容: py.type( string(missing) )

It returns <class 'NoneType'>它返回<class 'NoneType'>

MATLAB to python data types documentation here MATLABpython数据类型文档在这里

If you want to pass None into a Python function that you are calling from MATLAB , then you would pass in string(missing) .如果你想将None传递给你从 MATLAB 调用的Python MATLAB ,那么你将传递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 .此参数在Python function 中将显示为None ,例如,如果您正在检测None ,例如if arg1 == None

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM