Main Content

Ignore Inputs in Function Definitions

This example shows how to ignore inputs in your function definition using the tilde (~) operator. Use this operator when your function must accept a predefined set of inputs, but your function does not use all of the inputs. Common applications include defining callback functions.

In a file named colorButton.m, define a callback for a push button that does not use the eventdata input. Add a tilde to the input argument list so that the function ignores eventdata.

function colorButton
figure;
uicontrol('Style','pushbutton','String','Click me','Callback',@btnCallback)

function btnCallback(h,~)
set(h,'BackgroundColor',rand(3,1))

The function declaration for btnCallback is effectively the same as the following:

function btnCallback(h,eventdata)

However, using the tilde prevents the addition of eventdata to the function workspace and makes it clearer that the function does not use eventdata.

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

function myFunction(myInput,~,~)

Related Topics