MATLAB: How to uncheck checkbox

checkbox

i have checkbox and axes to display image. when i click checkbox1 it will apply noise to the image.how can i uncheck the checkbox1 to remove only the noise and click another checkbox to apply other noise.here my code for every checkbox.
fucntion checkbox1_callback(hObject,eventdata,handles)
if isChecked==1
if isfield(handles,'imgData)
I=handles.imgData;
J=imnoise(I,'salt & pepper',0.3);
imshow(J);
title('Noise type: Salt and pepper');
end
guidata(hObject,handles)
end

Best Answer

Hello amira, you could add another condition to your callback, like this:
fucntion checkbox1_callback(hObject,eventdata,handles)
if hObject.Value==1 % This is like your isChecked==1
if isfield(handles,'imgData')
I=handles.imgData;
J=imnoise(I,'salt & pepper',0.3);
imshow(J);
title('Noise type: Salt and pepper');
elseif hObject.Value==0
if isfield(handles,'imgData')
I=handles.imgData;
imshow(I)
title('No noise')
end
guidata(hObject,handles)
end
Note that I have changed isChecked to hObject.Value because isChecked is a variable that does not exist when you call the callback, and hObject.Value or handles.checkbox1.Value (which are equivalent if you are inside the callback function of checkbox1) they give you value 1 if it's checked and 0 if it's not checked.
Now if you want to apply different types of noise then you need to create for example a second checkbox, and let's suppose it's tag is checkbox2, then you could do like this:
fucntion checkbox2_callback(hObject,eventdata,handles)
if hObject.Value==1
if isfield(handles,'imgData')
I=handles.imgData;
J=imnoise(I,'my_noise',0.3);
imshow(J);
title('Noise type: mynoise');
elseif hObject.Value==0
if isfield(handles,'imgData')
I=handles.imgData;
imshow(I)
title('No noise')
end
guidata(hObject,handles)
end
Now the problem is that if you want to apply only one noise at a time and not allow 2 noises to be together you could do like this:
fucntion checkbox1_callback(hObject,eventdata,handles)
if hObject.Value==1 && handles.checkbox2.Value==0
if isfield(handles,'imgData')
I=handles.imgData;
J=imnoise(I,'salt & pepper',0.3);
imshow(J);
title('Noise type: Salt and pepper');
elseif hObject.Value==0 && handles.checkbox2==0
if isfield(handles,'imgData')
I=handles.imgData;
imshow(I)
title('No noise')
end
guidata(hObject,handles)
end
fucntion checkbox2_callback(hObject,eventdata,handles)
if hObject.Value==1 && handles.checkbox1.Value==0
if isfield(handles,'imgData')
I=handles.imgData;
J=imnoise(I,'my_noise',0.3);
imshow(J);
title('Noise type: mynoise');
elseif hObject.Value==0 && handles.checkbox1.Value==0
if isfield(handles,'imgData')
I=handles.imgData;
imshow(I)
title('No noise')
end
guidata(hObject,handles)
end
Like this you can only apply one noise at a time and also the checkboxes won't apply noise to an image that has allready a noise applied to it. If you want to add warning dialogs to warn the user that he is applying 2 noises then you could simply write them like this:
fucntion checkbox1_callback(hObject,eventdata,handles)
if hObject.Value==1 && handles.checkbox2.Value==0
if isfield(handles,'imgData')
I=handles.imgData;
J=imnoise(I,'salt & pepper',0.3);
imshow(J);
title('Noise type: Salt and pepper');
elseif hObject.Value==0 && handles.checkbox2==0
if isfield(handles,'imgData')
I=handles.imgData;
imshow(I)
title('No noise')
elseif hObject.Value==1 && handles.checkbox2==1
warndlg('Only one noise may be applied', 'Invalid choice warning');
end
guidata(hObject,handles)
end
fucntion checkbox2_callback(hObject,eventdata,handles)
if hObject.Value==1 && handles.checkbox1.Value==0
if isfield(handles,'imgData')
I=handles.imgData;
J=imnoise(I,'my_noise',0.3);
imshow(J);
title('Noise type: mynoise');
elseif hObject.Value==0 && handles.checkbox1.Value==0
if isfield(handles,'imgData')
I=handles.imgData;
imshow(I)
title('No noise')
elseif hObject.Value==1 && handles.checkbox1==1
warndlg('Only one noise may be applied', 'Invalid choice warning');
end
guidata(hObject,handles)
end
Hope this answer helped you get what you were looking for.
Related Question