Matlab R2009b introduced a new "operator" - ~ - 表示未使用的函数输出或输入。我对该实现有详细的疑问。 (调用所有 @Loren s。)
对于未使用的输入参数的值,函数会看到什么?
即如果我的函数定义为
myfunc(argOne, argTwo, argThree)
它的名字是这样的:
myfunc('arg', ~, 'arg')
nargin 是 2 还是 3? argTwo 是未定义的还是空的还是其他什么?
谢谢
最佳答案
~
语法仅适用于函数定义的输入,不适用于函数调用的输入(如 this documentation page 中所讨论).换句话说,这个没问题:
function myfunc(argOne, ~, argThree) %# Will do nothing with the second input
%# Do stuff here
end
但是这不是:
myfunc('arg', ~, 'arg'); %# Error city ;)
因此,调用函数时,只能使用左侧的~
:
[~, I] = sort([2 4 1 2 5 3]); %# Sort the vector and keep only the index
关于Matlab 未使用的输入符号详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2002542/