필터 지우기
필터 지우기

Button Group / make radiobutton exclusive

조회 수: 6 (최근 30일)
Max Müller
Max Müller 2014년 9월 24일
편집: Adam 2014년 9월 25일
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
Max Müller
Max Müller 2014년 9월 24일
yes i did,but my radiobutton had already Callbacks. Is this the reason it dont works ?
Adam
Adam 2014년 9월 24일
편집: Adam 2014년 9월 24일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Adam
Adam 2014년 9월 24일
편집: Adam 2014년 9월 24일
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
Adam
Adam 2014년 9월 25일
편집: Adam 2014년 9월 25일
The 'Value' field for each individual radiobutton controls its state so you can set all radiobuttons to a Value of 0 to achieve this. It appears nothing stops you setting all of them to 1 either though I thought I remembered the mutual exclusivity kicking in here when you set one to automatically unset the other.
Personally I would not recommend setting all to 0 as an initial state (and obviously never set all or more than one to a non-zero value).
This represents what would normally be considered an invalid state for radio buttons and one which can never be returned to after the user makes a selection.
If you really want this I would probably recommend you add a checkbox which disables the radiobuttons in the case where you want none of them to be active. This then maintains the internal consistency of the radiobuttons in all situations rather than having an odd state that can only occur at initialisation.
I have never tried it because it is a very bad idea, but I imagine you may be able to over-ride the mutual exclusivity control on radio buttons by setting their individual 'Value' fields programatically at a later stage too, but don't go down that route!!
Max Müller
Max Müller 2014년 9월 25일
ok. u are right

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by