Pushbutton GUI for code
Show older comments
Hi everybody, Im looking to add a GUI to my code in the form of a push button menu. I need 4 buttons with the output of the button being the option number, so 1, 2, 3 or 4. Any Help
Thanks
4 Comments
Rik
on 6 Dec 2018
Please don't close a question that has an answer. If my answer doesn't work for you, then please post comment which part doesn't work, or what is not clear to you.
If it does work, please use the 'accept this answer' button.
madhan ravi
on 6 Dec 2018
@Jake Harris it's really impolite to close a question after someone takes effort to answer you .
jake harris
on 6 Dec 2018
Image Analyst
on 6 Dec 2018
Did you see my answer below about using menu()?
Answers (2)
Rik
on 5 Dec 2018
1 vote
I would encourage you to just try out some code. You should look into the uicontrol function to create buttons. You can use the callback of those buttons (or a dropdown menu) to determine the code to be run. I would strongly recommend not using GUIDE (the builtin UI designer), as the code it generates is fairly complicated, is much harder to maintain, and is very complex to debug.
My small guide to avoid GUIDE:
- Make a figure (with f=figure;) and look into the doc for figure which properties you want to turn off (you probably want to set Menu and Toolbar to 'none')
- Create buttons and axes and everything you need with functions like uicontrol and axes. Save the handles to each element to fields of a struct (like handles.mybutton=uicontrol(___);)
- When you've finished loading all data (and saving it to fields of your handles struct), and creating all the buttons, save your handles struct to the guidata of your figure like this guidata(handles.f,handles);. (You can also use getappdata and setappdata)
- You can set the Callback property of many objects. If you do, use a function name with an @ in front, or a char array that can be evaluated to valid code. (like @MyFunction or 'disp(''you pushed the button'')')
- Callback functions will be called with two arguments: the first is a handle to the callback object, the second is eventdata that may contain special information. To get access to your data, just use handles=guidata(gcbo);. You can replace the gcbo function with the name of the first input to your callback function if you prefer.
- More information about callbacks can be found in multiple places in the doc, for example here.
1 Comment
madhan ravi
on 6 Dec 2018
+1 really informative!
Image Analyst
on 6 Dec 2018
There is already a built-in function for this. It's called, perhaps somewhat inappropriately, menu(). For example:
buttonNumber = menu(message, 'Option 1', 'Button 2 text', 'Choice #3', 'Quit / return', 'Call Jake - he will know what to do', 'None of these');
The buttonNumber will be the number (1,2,3, etc.) of the button they clicked on. Of course, modify the button captions to whatever you want them to say.
Categories
Find more on Creating, Deleting, and Querying Graphics Objects 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!