How to write GUI pushbutton callback function when you have the code(with no GUI)
Show older comments
I have the code for a game that runs well, but I want to add a graphical user interface(GUI) to that. To start with, I created a pushbutton(New Game) and pasted the entire code under the "function pushbutton1_Callback(hObject, eventdata, handles)". I thought I could run the game code by clicking the New Game button, but it didn't work. The error is "Undefined function or variable 'pushbutton1_Callback'." and "Error while evaluating UIControl Callback". So I'm wondering if there are certain special rules in writing callback function. How do you do it when you already have a code? Thank you!!
Answers (1)
Walter Roberson
on 28 Nov 2017
It does not take much.
function MyGame
pushbutton1 = uicontrol('Style', 'push', 'String', 'New game', 'Callback', @pushbutton1_Callback);
function pushbutton1_Callback(hObject, eventdata)
... now put some code for starting the game here
Those two can go into MyGame.m or you can put the function pushbutton1_Callback into pushbutton1_Callback.m as long as it is the first function of that separate .m file.
Categories
Find more on App Building 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!