MATLAB GUI Radiobutton Control
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Oguzhan Kirtas
el 20 de Mzo. de 2019
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.
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)
6 comentarios
Jan
el 21 de Mzo. de 2019
The explanation of the meaning of the GUI elements is not useful. Better explain what should happen, when you select a radiobutton or enter a value in the edit field. I still do not know, which behavior you want.
By the way,
if get(hObject,'Value')==get(hObject,'Max')
can be simplified for radiobuttons, which have the value 0 or 1 by definition. Then this is sufficient already:
if get(hObject,'Value')
Because 0 is treated as FALSE and 1 as TRUE.
I'm happy if your problem is solved.
Respuesta aceptada
Luna
el 21 de Mzo. de 2019
Editada: Luna
el 21 de Mzo. de 2019
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);
2 comentarios
Jan
el 21 de Mzo. de 2019
handles.edit1 is usually the habndle of the "edit1" field. Then it is not useful to test, if it is empty or to overwrite it with the value of the edit field.
Luna
el 21 de Mzo. de 2019
Ups! sorry! You are right Jan It will be:
if ~isempty(handles.edit1.String)
.
.
.
end
I just wrote it to not allow the user put empty string in that editbox for further calculations. It can also be written in a try catch block ofc.
Thanks for warning!
Más respuestas (0)
Ver también
Categorías
Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!