MATLAB GUI Radiobutton Control
이전 댓글 표시
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
I do not understand the question. while ~isa(handles.edit1,'double') is strange, because it checks the class of the graphics handle. Handles are not doubles anymore since R2014a. It is not clear, where you have inserted this test, but it would trigger only, if you change trhe Matlab release during the GUI is working - what is impossible, of course.
res=3*handles.edit1 is strange also: Multiplying a graphics handle is not meaningful. Because you did not write a meaningful comment, it is impossible to guess the purpose of this code. Maybe you mean:
res = 3 * sscanf(handles.edit1.string, '%g', 1);
set(handles.res, 'String', sprintf('%g', res))
"I want to select the radiobutton first, then I will enter a value to the edit box" - but you can do this, when you want it. What exactly is the problem? Please edit the original question and insert an explanation of what you want to happen.
Rik
2019년 3월 20일
I second the advice of Jan.
To add my own thoughts: it looks like you should have the callback of the radiobuttons clear the input fields, and then have the callback of the input field do the actual calculation.
Oguzhan Kirtas
2019년 3월 20일
If you use GUIDE, the name of the callback "edit1_Callback" implies, that the edit field has the tag "edit1" and the handle is stored in handles.edit1. Then overwriting this handle by the contents of the edit field is at least confusing. Converting handles into doubles is too indirect and too complicated. Why not using a new field of the handles struct to carry the data?
The radiobutton callback should not wait for another object. Simply insert the code in the callback of the edit field.
What should happen, when the edit field has been edited?
What is the purpose of teh radio button? Usually radiobutton groups are used for a mutually exclusive selection.
Oguzhan Kirtas
2019년 3월 21일
Jan
2019년 3월 21일
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.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!