Button Group / make radiobutton exclusive

16 views (last 30 days)
Max Müller
Max Müller on 24 Sep 2014
Edited: Adam on 25 Sep 2014
Hey guys, i have create a bunch of radiobuttons in one GUI. Now I want to make each radiobutton exclusive.... only one button can be active... if the use activates a new radiobutton the old one`s value is 0 (inactive). i have tried to use the SelectionChangeFcn but somehow it doesnt work. Can u guys give me an example with 2 radiobuttons in one panel ? i am going on to search for a solution.
Oh and pls use GUIDE in ur explaination.
  3 Comments
Adam
Adam on 24 Sep 2014
Actually I just took a look at creating a new radio button group. It looks like it has changed in recent versions of Matlab so you don't have to do so much manually.
Now when you drop radio buttons into a group your callback will be set to @manageButtons as shown below. You should be able to leave that as is and just add a SelectionChangeFcn. In previous versions I seem to remember having to get rid of callbacks manually when I added a SelectionChangeFcn

Sign in to comment.

Accepted Answer

Adam
Adam on 24 Sep 2014
Edited: Adam on 24 Sep 2014
This is a simple example from some code I just put together. A mutually exclusive radio box with two options and the SelectionChangeFcn that just prints to command window which is selected. Obviously you can do more complex stuff and store things on handles as usual within the callback.
% --- Executes when selected object is changed in uipanel1.
function uipanel1_SelectionChangeFcn(hObject, eventdata, handles)
str = get( hObject, 'String' );
if strcmp( str, 'Button 1' )
disp( 'Button 1' )
else
disp( 'Button 2' )
end
If it is useful here are a couple of images of the same in guide. There are other ways of programming the SelectionChangeFcn. Sometimes I use the radiobutton tags in my switch, but the above works ok. In general I think I would favour using tag though than strcmp against the string. The example works the same, just get 'Tag' instead of 'String'. It is less prone to typos than comparing a string though also a little less friendly to read.
%
  5 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by