Callback Push Button Execution

15 views (last 30 days)
pb on 27 Jul 2020
Edited: pb on 9 Aug 2020
In my code I have a Push Button, that will stay open during code execution. The user may use the push button; and that will pass some values to a function.
The function runs fine when run on it's own, with a call that looks like
In the code fo the push button, the format is this:
ButtonHandle2 = uicontrol(figure1,'Style','PushButton','Callback',@function)
I am not conceptually strong on buttonhandles (this is my first day using them), nor on function handles '@', and to me I originally thought one of these two should have happened:
ButtonHandle2 = uicontrol(figure1,'Style','PushButton','Callback',function(input1,input2,input3))
f = @function(input1,input2,input3)
ButtonHandle2 = uicontrol(figure1,'Style','PushButton','Callback',f)
However, neither of the two even executed. The format posted above actually executed the function, and I got this error:
Operator '-' is not supported for operands of type 'matlab.ui.eventdata.ActionData'.
Error in speedselect (line 6)
min_diff = abs(speedarray(1) - speed);
Error while evaluating UIControl Callback.
Since this is saying that the subtraction itself is not supported, I suppose the issue here is that I don't have enough familiarity with using callback functions with MATLAB and I couldn't see anything online that says that subtraction/addition is not supported, so I think it's a usage issue by me. I can easily work around this error with different structures of code, but I am curious to learn more about this conceptually. Please let me know if you can point out what I am doing wrong, and if this requires seeing more of my relevent code, I can post it.

Accepted Answer

Stephen23 on 27 Jul 2020
"...and to me I originally thought one of these two should have happened..."
What actually occurs is described in the MATLAB documentation:
The second section explains "Graphics callback functions must accept at least two input arguments" and describes exactly what those two default input arguments are. So your callback function must accept them both, just as the docs state. Also, if you are to perform any operations with them, you must use methods/operations that are suitable for those object types. Your code tries to perform some numeric operation on a graphics event object, which is clearly not valid.
These objects give information on the graphics object that triggered the callback, and the type of event. For any GUI that has more than one GUI object this information is very useful for writing neat and efficient code. But to get you started you can simply define the callback function as an anonymous function and ignore the two default input arguments:
in1 = ...
in2 = ...
in3 = ...
fun = @(~,~) myfun(in1,in2,in3);
% ^^^ ignore default objects
and then define the callback simply as:
ButtonHandle2 = uicontrol(figure1,'Style','PushButton','Callback',fun)
However those input values will be bound to the anonymous function when it is defined, which might not be what you want. So most likely you will also need to pass the data within the GUI, as explained in the documentation:
If you are sensibly writing your own GUI code then I recommend using nested functions as a simple and intuitive way to pass data around inside a GUI. Changing the inputs to a callback when it is called is unlikely to be a useful approach. Avoid global variables.
  1 Comment
pb on 9 Aug 2020
Thank you very much! Great explanation

Sign in to comment.

More Answers (1)

Rik on 27 Jul 2020
Edited: Rik on 27 Jul 2020
A callback has the object handle as the first input and the eventdata as the second input. You can use the object handle to retrieve the guidata struct.
For more information about how to create a GUI and relevant documentation links, see this thread .
  1 Comment
pb on 9 Aug 2020
Edited: pb on 9 Aug 2020
Thank you very much! I didn't realize about the reserved inputs

Sign in to comment.


Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!