MATLAB: MATLAB GUI Radiobutton Control

guiradio button

I'm writing a MATLAB GUI code that includes radiobuttons and corresponding edit boxes. To explain my problem better, I created a basic GUI as in the figure. If I first enter a value to the edit box and select the corresponding radiobutton, I obtain the result. However, I want to select the radiobutton first, then I will enter a value to the edit box. I tried several ways including while loops until the condition is met "while ~isa(handles.edit1,'double') end" and waitfor function but I could not solve the problem. I am adding the relevant parts of the code as well.
radiobutton.JPG
function radiobutton_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
set(handles.radiobutton1,'Value',0);
set(handles.radiobutton2,'Value',0);
guidata(hObject, handles);
function varargout = radiobutton_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
if get(hObject,'Value')==get(hObject,'Max')
res=3*handles.edit1;
set(handles.res,'String',res);
guidata(hObject, handles);
end
% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
if get(hObject,'Value')==get(hObject,'Max')
res=3*handles.edit2;
set(handles.res,'String',res);
guidata(hObject, handles);
end
function edit1_Callback(hObject, eventdata, handles)
handles.edit1=str2double(get(hObject,'String'));
guidata(hObject, handles);
function edit1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit2_Callback(hObject, eventdata, handles)
handles.edit2=str2double(get(hObject,'String'));
guidata(hObject, handles);
function edit2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function uibuttongroup1_SelectionChangedFcn(hObject, eventdata, handles)
function res_CreateFcn(hObject, eventdata, handles)

Best Answer

Hi Oğuzhan,
You can just simply do this:
When option1 selected, enable edit1 and disable edit2. (radiobutton1 callback)
set(editbox1handle,'Enable','on');
set(editbox2handle,'Enable','off');
When option2 selected, enable edit2 and disable edit1. (radiobutton2 callback)
set(editbox1handle,'Enable','off');
set(editbox2handle,'Enable','on');
Implement these into your radiobutton callbacks seperately. So that user never can edit the other option's editbox and code never executes the unrelevant editbox' callback because you will be sure that it won't be changed when it is disabled.
You should only use radiobutton callbacks to control the behaviour of your GUI, not impelement any mathematical calculations or never get any editbox value in your radiobutton callbacks.
Implement your mathematical calculations in your editbox callbacks. So whenever related one edited by user, you get the mathematical operations.
Just for an example. Read my comments and you will get the idea.
function radiobutton1_Callback(hObject, eventdata, handles)
set(handles.edit1,'Enable','on');
set(handles.edit2,'Enable','off');
% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
set(handles.edit1,'Enable','off');
set(handles.edit2,'Enable','on');
function edit1_Callback(hObject, eventdata, handles)
% handles.edit1=str2double(get(hObject,'String'));
% you can only check if is empty then convert it to double
% do the same thing in your edit2_callback
if ~isempty(handles.edit1.String)
handles.edit1=str2double(get(hObject,'String'));
res=3*handles.edit1;
handles.res = res; % you can reach handles any callback when you want to get res.
end
%% I don't know why you check this max??
% if get(hObject,'Value')==get(hObject,'Max')
% res=3*handles.edit1;
% set(handles.res,'String',res);
% guidata(hObject, handles);
% end
% guidata(hObject, handles);
Related Question