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 namedcolorButton.m
, define a callback for a push button that does not use theeventdata
input. Add a tilde to the input argument list so that the function ignoreseventdata
.
functioncolorButton figure; uicontrol('Style','pushbutton','String','Click me','Callback',@btnCallback)functionbtnCallback(h,~) set(h,'BackgroundColor',rand(3,1))
The function declaration forbtnCallback
is effectively the same as the following:
functionbtnCallback(h,eventdata)
However, using the tilde prevents the addition ofeventdata
to the function workspace and makes it clearer that the function does not useeventdata
.
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:
functionmyFunction(myInput,~,~)