views:

61

answers:

1

Matlab R2009b introduced a new "operator" - ~ - to symbolize an unused function output or input. I have detailed question about that implementation. (Calling all @Loren s.)

What does the function see for the value of an unused input parameter?

i.e. if my function is defined as

myfunc(argOne, argTwo, argThree)

and it’s called like this:

myfunc('arg', ~, 'arg')

Is nargin 2, or 3? Is argTwo undefined or empty or something else?

Thanks

+3  A: 

The ~ syntax is only applicable to the inputs of the function definition, not the inputs of the function call (as discussed on this documentation page). In other words, this is OK:

function myfunc(argOne, ~, argThree)  %# Will do nothing with the second input
  %# Do stuff here
end

but this is not:

myfunc('arg', ~, 'arg');  %# Error city ;)

So, when calling a function, you can only use ~ on the left-hand side:

[~, I] = sort([2 4 1 2 5 3]);  %# Sort the vector and keep only the index
gnovice
The ~ can be used in a function _call_ if it's on the LHS. E.g. "[~,~,x] = junk(1)" works. Nargout is the same as if regular variables had been used.
Andrew Janke
@Andrew: Noted, and thanks. I rephrased so that hopefully it is clearer now.
gnovice

related questions