Note: The bold_checkbox_Callback function triggers when the user activates the check box AND when the user deactivates the check box.
14. Now that we ve completed both the visual and code aspects of the GUI, its
time to run the GUI to make sure it works before we move on. Try checking and unchecking the Check Box component to make sure that the text “Hello World!” is being bolded and unbolded.
And that s it. Those are the basics of using the Check Box component. Now we re ready to move onto the Button Group, which is the most challenging part of this tutorial.
Part Three: Radio Buttons, Toggle Buttons, and Button Group Panel
Radio buttons and Toggle buttons are used exactly the same way that check boxes are used in Matlab GUIs, so we won t go over how to use them. But there is one special case that needs to be covered. When either radio buttons or toggle buttons are used in conjunction with the button group panel, they exhibit mutually exclusive behavior. Simply put, this means that only one radio button or one toggle button
网上的对GUI控件的介绍!
can be selected at a time. This behavior can come in very useful for some GUIs. Since radio buttons and toggle buttons are identical in their functionality, what is said about one, is true for the other. Thus, only radio buttions will be discussed from here on out.
In this part of the tutorial, we will create a button group that will allow you to choose between different font sizes for the display text.
1. The first thing we need to do is to add a Button Panel component to the GUI
figure that we were just working with. So if you closed GUIDE, reopen it again. Once you have GUIDE opened again, click on and add one Button Panel component to the GUI figure. Make sure it s large enough to fit in three radio buttons. Next, click on
2. and add three radio buttons onto the button group panel. Double click on the first Radio Button component to bring up the Property
Inspector. Change the String property to 8. Change the Tag property to
fontsize08_radiobutton.
Next, double click on the second Radio Button component, and change the String property to 12, and change the Tag property to fontsize12_radiobutton.
Next, double click on the third Radio Button component, and change the String property to 16, and change the Tag property to fontsize16_radiobutton.
Finally, double click on the button group panel and change the Tag property to fontSelect_buttongroup. You should also change the String property for the button group panel to Fontsize.
3. Here s what your figure should look like after you add the components and
modify them.
网上的对GUI控件的介绍!
4.
Before we move on, we should check the hierarchical structure of the GUI figure. Click on the
icon and the followinging should appear:
Make sure that the three radio buttons are one hierarchy below the button group icon. 5. Add the following line of code to the opening function. In this tutorial
example, it is named button_tutorial_OpeningFcn function. Yours will be the name of the file you saved it as, followed by “_OpeningFcn”. set(handles.fontSelect_buttongroup,'SelectionChangeFcn',@fontSelect_buttongroup_SelectionChangeFcn);
Make sure the previous line was added right before the line:
guidata(hObject, handles);
网上的对GUI控件的介绍!
Next, add the following function at the very end of the .m file.
function fontSelect_buttongroup_SelectionChangeFcn(hObject, eventdata)
%retrieve GUI data, i.e. the handles structure
handles = guidata(hObject);
switch get(eventdata.NewValue,'Tag') % Get Tag of selected object
case 'fontsize08_radiobutton'
%execute this code when fontsize08_radiobutton is selected
set(handles.display_staticText,'FontSize',8);
case 'fontsize12_radiobutton'
%execute this code when fontsize12_radiobutton is selected
set(handles.display_staticText,'FontSize',12);
case 'fontsize16_radiobutton'
%execute this code when fontsize16_radiobutton is selected
set(handles.display_staticText,'FontSize',16);
otherwise
% Code for when there is no match.
end
%updates the handles structure
guidata(hObject, handles);
6. Notice that the callback functions for the radio buttons were not
automatically generated by Matlab. This is completely normal. Each time a button is selected within the Button Group Panel component, the function defined within the SelectionChangeFcn property of Button Group Panel component is called. The line of code that was added in the opening function specifies the callback function when a button within the button group is selcted. The selection change function is then defined at the end of the .m file.
7. Now that we ve completed both the visual and code aspects of the GUI, its
time to run the GUI again. Try clicking on all of the buttons to make sure they