Introduction
In this Matlab GUI Tutorial, you will learn how to use radio buttons, toggle buttons and the 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 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.
-
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
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 tofontsize08_radiobutton.
Next, double click on the second Radio Button component, and change the String property to
12, and change the Tag property tofontsize12_radiobutton.Next, double click on the third Radio Button component, and change the String property to
16, and change the Tag property tofontsize16_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 toFontsize. -
Here’s what your figure should look like after you add the components and modify them.

-
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.
-
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);
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); -
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.
-
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 perform their function correctly. Specifically, make sure that the font size changes accordingly.

And that’s it. Those are the basics of using the different buttons within the Matlab GUI.
Thanks! it was v informative and helpful!
hello
thank for your solution,
but there is some problems in it that i suggest that you correct them whit the solution @link below:
http://blinkdagger.com/matlab/matlab-gui-tutorial-buttons-button-group/
Your tutorials are very helpful. Thank you so much!!
I’m a little rusty with MATLAB and am currently working on making a GUI for the interface of my senior design project. I am unsure of which type of buttons to use and I have been using the radio buttons for my application right now. I want the user to input their response for three questions using the options provided with the radio buttons. However, I am able to select more than one radio button at one time. I’m unsure how to change this. Also, I need the responses recorded by the user to be recorded in variables. Would you have any advice for me? Thanks.