Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct use of tilde operator for input arguments

Function:

My MATLAB function has one output and several input arguments, most of which are optional, i.e.:

output=MyFunction(arg1,arg2,opt1,opt2,...,optN)

What I want to do:

I'd like to give only arg1, arg2 and the last optional input argument optN to the function. I used the tilde operator as follows:

output=MyFunction(str1,str2,~,~,...,true)

Undesired result:

That gives the following error message:

Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.

The error points to the comma after the first tilde, but I don't know what to make of it to be honest.

Problem identification:

  • I use MATLAB 2013b, which supports the tilde operator.

  • According to MATLAB's documentation the above function call should work:

    You can ignore any number of function inputs, in any position in the argument list. Separate consecutive tildes with a comma...

  • I guess there are a few workarounds, such as using '' or [] as inputs, but I'd really like to understand how to correctly use '~' because actually leaving inputs out allows me to use exist() when checking the input arguments of a function.

If you need any further info from me, please let me know.

Thank you very much!

like image 346
DMueller Avatar asked Aug 08 '14 12:08

DMueller


People also ask

What does the tilde do in MATLAB?

Tilde ~ is the NOT operator in Matlab, and it has nothing special with images, it just treats them as matrices. ~ as operator return a boolean form of the matrix it's called against, that the result matrix is 1 for 0 in the original matrix and 0 otherwise.

How do you validate inputs in MATLAB?

Use function argument validation in MATLAB® to declare specific restrictions on function input arguments. You can constrain the class, size, and other aspects of function input values without writing code in the body of the function to perform these tests.

How do you validate the number of function arguments in MATLAB?

nargin in Argument ValidationThe nargin function returns the number of function input arguments given in the call to the currently executing function. When using function argument validation, the value returned by nargin within a function is the number of positional arguments provided when the function is called.


2 Answers

The tilde is only for function declaration. Matlab's mlint recommends to replace unused arguments by ~. The result is a function declared like this function output = MyFunction(a, b, ~, c). This is a very bad practice.

Since you have a function where the parameters are optional, you must call the function with empty arguments output=MyFunction(str1,str2,[],[],...,true).

A better way to do it is to declare the function with the varargin argument and prepare your function for the different inputs:

function output = MyFunction(varargin)

if nargin == 1
    % Do something for 1 input
elseif nargin == 2
    % Do something for 3 inputs
elseif nargin == 3
    % Do something for 3 inputs
else
    error('incorrect number of input arguments')
end

It is even possible to declare your function as follows:

function output = MyFunction(arg1, arg2, varargin)

The declaration above will tell Matlab that you are expecting at least two parameters.

See the documentation of nargin here.

... and the documentation of varargin here

like image 113
gire Avatar answered Oct 26 '22 06:10

gire


To have variable number of inputs, use varargin. Use it together with nargin.

Example:

function varlist2(X,Y,varargin)
   fprintf('Total number of inputs = %d\n',nargin);

   nVarargs = length(varargin);
   fprintf('Inputs in varargin(%d):\n',nVarargs)
   for k = 1:nVarargs
      fprintf('   %d\n', varargin{k})
   end
like image 23
lakshmen Avatar answered Oct 26 '22 08:10

lakshmen