Question

I am using matlab GUI. I have 3 checkbox(A, B, C) placed on a uipanel. I want selection of 1 checkbox at a time (e.g when A is selected B and C should be automatically deselected. I know this can be achieved with radio buttons but I am also looking for no-checkbox selection which is why I am not using radio button. So here are two things i am looking for:

1) Have only one checkbox selected at a time with other two being automatically deselected. 2) Allow for no checkbox selection (this is available by default and is the reason I am not using radio buttons)

I can't seem to figure out. I have inserted a uipanel and used the following code but this doesn't work. Any ideas or alternatives??

function uipanel1_Callback(hObject, eventdata, handles)
    handles = guidata(hObject); 
    checkbox1=get(handles.checkbox1,'Value');
    checkbox2=get(handles.checkbox2,'Value');

    set(handles.edit1,'String',checkbox1); %just for checking if working or not

   if checkbox1==1 
           set(handles.checkbox2,'Value',0); 
           set(handles.checkbox1,'Value',1); 
           set(handles.edit1,'String','check1'); %just for checking 
   end
   if checkbox2==1 
         set(handles.checkbox1,'Value',0); 
         set(handles.checkbox2,'Value',1); 
   end

  % Update handles structure
  guidata(hObject, handles);
Was it helpful?

Solution

you can use radio button group and add one more option ("no selection") or you can use radio button group with 3 options and add button to set all option to off

if you want to use checkbox it works when you write callbacks for each checkbox instead of uipanel (I'm affraid callback for uipanel is not evaluated at all) (here I assume that in MyUipanel there are only checkboxes):

function MyCheckboxCallback(hObject, eventdata, handles)
other = setdiff(get(handles.MyUipanel,'Children'),hObject);
for ii = 1:length(other)
    set(other(ii),'Value',get(other(ii),'Min'));
end

function MyCheckbox1_Callback(hObject, eventdata, handles)
MyCheckboxCallback(hObject, eventdata, handles)

function MyCheckbox2_Callback(hObject, eventdata, handles)
MyCheckboxCallback(hObject, eventdata, handles)

function MyCheckbox3_Callback(hObject, eventdata, handles)
MyCheckboxCallback(hObject, eventdata, handles)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top