Creating Gui in matlab

35 次查看(过去 30 天)
vedesh Mohit
vedesh Mohit 2019-9-24
评论: Adam 2019-9-24
Hey, I am creating a drop menu in matlab GUI with logic gates options 2 input OR, AND, NOT, NAND & NOR gates. I would like to know how to code this so when I select an option lets say an OR gate, the OR gate image is placed in a window to actually allow construction of a circuit.
  4 个评论
Rik
Rik 2019-9-24
You can use the callback functions.
Adam
Adam 2019-9-24
If you use GUIDE callbacks should be automatically created unless you have explicitly switched that off. If you are doing it programmatically you can just create a callback function and pass its handle as the 'callback' property. Either way you can put your chosen code in there. If it is a simple GUI then you can do it very quickly in GUIDE, using the handles struct that you have access to in every callback, which contains the handles of all other UI components too.
gives details of how you can communicate between different callbacks.
Doing a programmatic GUI it depends how you are storing all your components and organising them. Personally I use a class as the basis for my GUIs which makes it trivial to share the different components amongst the callbacks, which are methods of the class.

请先登录,再进行评论。

回答(1 个)

Rik
Rik 2019-9-24
I would recommend not using GUIDE. The point where GUIDE will be removed from Matlab is now on the horizon, and you shouldn't be using it for any moderately complex project anyway.
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(___);)
  • Use those handles in function calls that create graphics objects. Never use gcf and gca in your code, because the user might have clicked on a different figure window, making those call targets incorrect. Most functions allow you to specify a parent object (including, plot, bar, axes and many more). When creating a graphics object, check the documentation to see how you can specify the parent.
  • 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.

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by