Pushbutton GUI for code

9 ビュー (過去 30 日間)
jake harris
jake harris 2018 年 12 月 5 日
コメント済み: Image Analyst 2018 年 12 月 6 日
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 件のコメント
jake harris
jake harris 2018 年 12 月 6 日
Sorry new to the whole forum thing. I do appreciate somebody aswering i'm just not used to the Forum format. But now i know :)
Image Analyst
Image Analyst 2018 年 12 月 6 日
Did you see my answer below about using menu()?

サインインしてコメントする。

回答 (2 件)

Rik
Rik 2018 年 12 月 5 日
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 件のコメント
madhan ravi
madhan ravi 2018 年 12 月 6 日
+1 really informative!

サインインしてコメントする。


Image Analyst
Image Analyst 2018 年 12 月 6 日
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.

カテゴリ

Help Center および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by