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, as shown here, or deriving a class from a superclass.
Define a callback for a push button in a file named colorButton.m
that
does not use the eventdata
input. Ignore the input
with a tilde.
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 essentially the
same as
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 function inputs, in any position in the argument list. Separate consecutive tildes with a comma, such as
myfunction(myinput,~,~)