Programming the gUI to associate the component with the function. By default, Tag is set to a generic label such as pushbutton. You might want to change the tag property of the pop-up menu to a more descriptive label, such as plot_ popup as shown in the following figure before you save or run the gui for the first time D哆回易咕鹦口B廖函哈F 圆 Push Button ao uncontrol(plot popup"peac) SelectionHighlight Sidestep popupmenu plot popup Set Tag to plot_popup Itipstring UIContexMenu -<None> When you save or run the GUl, GUidE sets the pop-up menu Callback property to plot_popup_callback. If you later change the Tag, GUIDE updates the Callback property to match the new Tag- see"Changing Component Tag Properties"on page 3-44. Similarly, change the push button tags to surf_pushbutton, mesh_pushbutton, nd contour _ pushbutton To learn more, see "Setting Component Properties- The Property Inspector on page 4-14 Opening the GUI M-File all the callbacks. See"GUI FIG-Files and M-Files"on page 1-5. For each Eains When you first save or run the gui, guide generates an M-file that cor component that has a callback associated with it, guide generates a framework for the callback, containing just a function definition. You must then add code to the callbacks to make them work. In this example, you add
Programming the GUI 2-13 to associate the component with the function. By default, Tag is set to a generic label such as pushbutton1. You might want to change the Tag property of the pop-up menu to a more descriptive label, such as plot_popup, as shown in the following figure, before you save or run the GUI for the first time: When you save or run the GUI, GUIDE sets the pop-up menu Callback property to plot_popup_Callback. If you later change the Tag, GUIDE updates the Callback property to match the new Tag - see “Changing Component Tag Properties” on page 3-44. Similarly, change the push button tags to surf_pushbutton, mesh_pushbutton, and contour_pushbutton. To learn more, see “Setting Component Properties — The Property Inspector” on page 4-14 Opening the GUI M-File When you first save or run the GUI, GUIDE generates an M-file that contains all the callbacks. See “GUI FIG-Files and M-Files” on page 1-5. For each component that has a callback associated with it, GUIDE generates a framework for the callback, containing just a function definition. You must then add code to the callbacks to make them work. In this example, you add Set Tag to plot_popup
Example: Creating a GUI code to the callbacks for the three push buttons and the pop-up menu. For more nformation, see"Understanding the guI M-File"on page 5-2. You can edit the M-file code by clicking the M-file Editor icon A on the toolbar If you previously saved or ran the GUl, pressing the M-file Editor icon displays the M-file in the matlab editor. Otherwise, guide does two things Generates the M-file associated with the gui Opens the Save GUi as dialog Type a name for the FIG-file in the File name field GuidE assigns the same name to the M-file. When you click Save, GUIDE saves the M-file and opens it in the m-file editor You can view a list of the callbacks in the M-file by clicking the function icon f, on the toolbar as sh in the following figure File Edt Vew Text Debug Breakponts Web window Heb 0回x鹛的。母.日伯的轴x 1 function varargout= sinple_gu simple_g A SIMPLE GUI Application M-fil contour pushbutton C JT. SIMPLE_ GUI Create mesh_pushbuttonCallback argunents ct plot popup_ca‖back Click the SIMPLE_GUI Create the Gu simple_gui_OpeningFcn imple gui_Output cn 〔obok Simple gulrig ecogmized Proper tyNan/Propertyvalue p 3 passed to sinple gui openingFcn using varargin. 124 see also: GUIDE, GCBO, OPTHFIG, GUIDATA, GUTHANDLES, FEVAL t Edit the above text to nodify the re to help simple gui simple gui/ select p. Ln 97 Col 21 Clicking a callback in the pop-up menu moves the cursor in the editor to the first line of the callback. For example, click simple gui Opening Fcn to display the opening function, as shown in the following figure 2-14
2 Example: Creating a GUI 2-14 code to the callbacks for the three push buttons and the pop-up menu. For more information, see “Understanding the GUI M-File” on page 5-2. You can edit the M-file code by clicking the M-file Editor icon on the toolbar. If you previously saved or ran the GUI, pressing the M-file Editor icon displays the M-file in the MATLAB editor. Otherwise, GUIDE does two things: • Generates the M-file associated with the GUI. • Opens the Save GUI as dialog. Type a name for the FIG-file in the File name field. GUIDE assigns the same name to the M-file. When you click Save, GUIDE saves the M-file and opens it in the M-file Editor. You can view a list of the callbacks in the M-file by clicking the function icon on the toolbar, as shown in the following figure. Clicking a callback in the pop-up menu moves the cursor in the editor to the first line of the callback. For example, click simple_gui_OpeningFcn to display the opening function, as shown in the following figure. Click the OpeningFcn Callback
Programming the gUI File Edt View Text Debug Breakpoints Web Window Heb 回的鹛。母f.日伯的图自su*m k End initialization code- DO NOT ED cn(hobject, eventdata, handle t. This function h output orgs, see DutputFcn reserved be detined in Iture version of MATLAB varargin unrecognized PropertyNane/Propertyvalue pairs Iron the counand line (see vARARGI) You can add code to the opening function to create data for the Gul, or perform other tasks, as described in the next section Opening Functi The code in the opening function is executed just before the GUi is made visible to the user. You can add code to the opening function to perform tasks that need to be done before the user has access to the gui, for example, to create data or to read data from an external source In this example you add code that creates three data sets in the opening function, using the matlAB functions peaks, membrane, and sinc. This code is described in the section"Adding Code to the Opening function"on page 2-16 Note that guidE names the opening function with the name of the M-file prefixed to openingFcn. In this example the M-file is named simple_ gu so that the opening function is named simple gui OpeningFcn Sharing Data Between Callbacks You can share data between callbacks by storing it in the MatlAB handles structure. For example, to store data contained in a vector X in the handle structure, you
Programming the GUI 2-15 You can add code to the opening function to create data for the GUI, or perform other tasks, as described in the next section. Opening Function The code in the opening function is executed just before the GUI is made visible to the user. You can add code to the opening function to perform tasks that need to be done before the user has access to the GUI, for example, to create data or to read data from an external source. In this example, you add code that creates three data sets in the opening function, using the MATLAB functions peaks, membrane, and sinc. This code is described in the section “Adding Code to the Opening Function” on page 2-16. Note that GUIDE names the opening function with the name of the M-file prefixed to _OpeningFcn. In this example, the M-file is named simple_gui.m, so that the opening function is named simple_gui_OpeningFcn. Sharing Data Between Callbacks You can share data between callbacks by storing it in the MATLAB handles structure. For example, to store data contained in a vector X in the handles structure, you
GUI 1 Choose a name for the field of the handles structure where you want to store the data, for example, handles. my_data 2 Set the field equal to X with the following command: 3 Save the handles structure with the guidata function Here, hobject is the handle to the object that executes the callback. Note To save any changes that you make to the handles structure, you must dd the command guidata (hobject, handles) following the code that implements the changes To retrieve x in another callback. use the command x= handles. my data You can access the data in the handles structure in any callback because hobject and handles are input arguments for all the callbacks generated by GUIDE For more detailed information on the handles structure. see ·“ Sharing Data with the Handles Structure” on page52 ·“ Managing gui Data with the Handles Structure” on page5-8 Adding Code to the Opening Function To create data for th he gui to pl lot add the code to the function immediately after the comments following the function de claration 9---Executes just before simple gui is made visible function simple gui OpeningFcn(hobject, eventdata, handles, varargin) 9 This function has no output args, see OutputFcr 9s eventdata reserved- to be defined in a future version of matlab les structure with handles and user data (see GUIDATA) 2-16
2 Example: Creating a GUI 2-16 1 Choose a name for the field of the handles structure where you want to store the data, for example, handles.my_data 2 Set the field equal to X with the following command: handles.my_data = X; 3 Save the handles structure with the guidata function: guidata(hObject, handles) Here, hObject is the handle to the object that executes the callback. Note To save any changes that you make to the handles structure, you must add the command guidata(hObject, handles) following the code that implements the changes. To retrieve X in another callback, use the command X = handles.my_data; You can access the data in the handles structure in any callback because hObject and handles are input arguments for all the callbacks generated by GUIDE. For more detailed information on the handles structure, see • “Sharing Data with the Handles Structure” on page 5-2 • “Managing GUI Data with the Handles Structure” on page 5-8 Adding Code to the Opening Function To create data for the GUI to plot, add the following code to the opening function immediately after the comments following the function declaration. % --- Executes just before simple_gui is made visible. function simple_gui_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
varargin command line arguments to untitled (see VARARGIN) Add this code Autogenerated code %s Create the data to plot handles peaks=peaks(35) handles membrane=membrane [X, y] =meshgrid(-8:5: 8); sart(x. 2+. 2)+ ep sinc sin(r)./r; handles sinc= sinc; handles. current data handles peaks The first six executable lines create the data using the matlAB functions peaks, membrane and sinc to generate the data. The next line, handles. current data= handles peaks, sets the current data field of the handles structure equal to the data for peaks. The value of handles. current data changes each time a user selects a different plot from the pop-up menu- see"Pop-up Menu Callback?"on page 2-19 The last line displays the surf plot for peaks, which appears when the gui is first opened GUidE automatically generates two more lines of code in the opening function, which follow the code that you add: handles output hobject saves the handle to the gui for later access by the output function. while this command is not necessary in this example, it is useful if you want to return the gui handle to the command line guidata(hobject, handles)saves the handles structure Adding Code to the callbacks property. For example, the callback for the Surf push button l ent'stag t o When the GUi is completed and running, and a user clicks a component of GUl, MATLAB executes the callback specified by the components callb property. The name of the callback is determined by the compor surf pushbutton Callback 2-17
Programming the GUI 2-17 % varargin command line arguments to untitled (see VARARGIN) % Create the data to plot handles.peaks=peaks(35); handles.membrane=membrane; [x,y] = meshgrid(-8:.5:8); r = sqrt(x.^2+y.^2) + eps; sinc = sin(r)./r; handles.sinc = sinc; handles.current_data = handles.peaks; surf(handles.current_data) The first six executable lines create the data using the MATLAB functions peaks, membrane and sinc to generate the data. The next line, handles.current_data = handles.peaks, sets the current_data field of the handles structure equal to the data for peaks. The value of handles.current_data changes each time a user selects a different plot from the pop-up menu — see “Pop-up Menu Callback” on page 2-19. The last line displays the surf plot for peaks, which appears when the GUI is first opened. GUIDE automatically generates two more lines of code in the opening function, which follow the code that you add: • handles.output = hObject saves the handle to the GUI for later access by the output function. While this command is not necessary in this example, it is useful if you want to return the GUI handle to the command line. • guidata(hObject, handles) saves the handles structure. Adding Code to the Callbacks When the GUI is completed and running, and a user clicks a component of the GUI, MATLAB executes the callback specified by the component’s Callback property. The name of the callback is determined by the component’s tag property. For example, the callback for the Surf push button is surf_pushbutton_Callback. Add this code Autogenerated code