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)

댓글 수: 6

Jan
Jan 2019년 3월 20일
편집: Jan 2019년 3월 20일
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
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.
Thank you for your reply. I obtain the values of edit1 and edit2 by converting input string to double with "str2double" as,
function edit1_Callback(hObject, eventdata, handles)
handles.edit1=str2double(get(hObject,'String'));
guidata(hObject, handles);
If I enter inputs of edit1 and edit2 first, then select the corresponding radiobuttons, res=3*handles.edit1 works since handles.edit1 is converted to double by callback function above. However, when I select the radiobutton first, the input value is an empty string and cannot be converted into double. So the calculation of res=3*handles.edit1 cannot be done. Even if I enter the input after selecting the radiobutton, it does not work.
What I want is, the code below waits for the inputs of edit boxes and does the calculation after.
function radiobutton1_Callback(hObject, eventdata, handles)
if get(hObject,'Value')==get(hObject,'Max') % if radiobutton1 is selected
% A code to wait until edit1 is entered by user
res=3*handles.edit1;
set(handles.res,'String',res);
guidata(hObject, handles);
end
Jan
Jan 2019년 3월 20일
편집: Jan 2019년 3월 21일
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
Oguzhan Kirtas 2019년 3월 21일
I am writing a code for the performance calculations of vehicles. Other than many parameters, I use two radiobutton groups: First one is the weight on drive wheels as kg or weight percent. Second one is the tire radius or revolutions per km. User selects one option in each radiobutton group. Weight on drive wheels, tire radius and tire revolutions are used in performance calculations. It's true that the code I shared looks meaningless, I just shared it in order to explain the problem that I encountered better.
I am new to GUIDE and your comments helped me greatly to solve the issue. I simply defined the state of radiobuttons in radiobutton callbacks (1 or 0) and moved the calculations to edit field callbacks. It works for my main code as well.
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

Luna
Luna 2019년 3월 21일
편집: Luna 2019년 3월 21일

0 개 추천

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

Jan
Jan 2019년 3월 21일
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.
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!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

질문:

2019년 3월 20일

편집:

2019년 3월 21일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by