How to use value entered in one edit text in another one?
2 次查看(过去 30 天)
显示 更早的评论
I created simple gui. It has three edit text tagged with aa, bb and cc and one static text tagged with r1. Result of computing with value of aa, bb and cc is printed out to static text. Result of computing is different if bb and cc are equal to zero so because of that I made condition
function aa_Callback(hObject, eventdata, handles)
% hObject handle to aa (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of aa as text
% str2double(get(hObject,'String')) returns contents of aa as a double
por=handles.bb+handles.cc;
aa = str2double(get(hObject,'String'));
if isnan(aa)
aa = 1;
set(hObject,'String',aa);
errordlg('Input must be a number', 'Error')
elseif aa==0
errordlg('Error.', 'Error');
elseif (aa~=0 && por==0)
set(handles.r1, 'String', 0);
end
handles.aa = aa;
guidata(hObject,handles)
This is for cc:
function cc_Callback(hObject, eventdata, handles)
% hObject handle to cc (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of cc as text
% str2double(get(hObject,'String')) returns contents of cc as a double
cc = str2double(get(hObject,'String'));
if isnan(cc)
cc=0;
set(hObject,'String',cc);
errordlg('Input must be a number', 'Error')
end
handles.cc = cc;
guidata(hObject,handles)
And this is for bb:
function bb_Callback(hObject, eventdata, handles)
% hObject handle to bb (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of bb as text
% str2double(get(hObject,'String')) returns contents of bb as a double
bb = str2double(get(hObject,'String'));
if isnan(bb)
bb =0;
set(hObject,'String',bb);
errordlg('Input must be a number', 'Error')
end
handles.bb = bb;
guidata(hObject,handles)
It works if I enter values in edit texts by this order bb than cc than aa or by this order cc than bb than aa. But if I enter aa first it doesnt work. Can someone help me to make to it works independently from oredr of typing values in edit texts?
2 个评论
JohnGalt
2016-5-19
the reason it doesn't work is because when you edit 'aa' and haven't yet edited 'bb' and 'cc' ... the fields/variables handles.aa and handles.bb are not yet set.
*Option1 * initialise the fields handles.aa and handles.bb
Option 2 Directly access the string (text) in the other edit boxes...
handles.figure1.Children(strcmp('bb',{handles.figure1.Children(:).Tag})).String
(this should work from inside any of the callbacks... it finds the object with tag 'bb' and returns the string in editbox 'bb').
The easy way to debug this type of stuff is to temporarily add a 'keyboard' command before the 'guidata' line in the callback... then you can inspect the variables accessible from the callback. (Which is what I did here.)
回答(2 个)
Geoff Hayes
2016-5-25
kh - you say that you have tagged your edit text fields as aa, bb, and cc. If this is true, then your handles structure should have three fields already named as aa, bb, and cc where each corresponds to the handle of the graphics control. So you don't want to overwrite this in your (for example) bb_Callback as
function bb_Callback(hObject, eventdata, handles)
% etc.
handles.bb = bb; % THIS IS WRONG - you are overwriting the graphics handle!!!
guidata(hObject,handles);
You don't want to overwrite the graphics handle. Instead, I would just access the edit text fields directly from within the aa_Callback and not rely on a value stored in the handles structure. In this case, your code would become
function aa_Callback(hObject, eventdata, handles)
bbValue = str2double(get(handles.bb,'String'));
ccValue = str2double(get(handles.cc,'String;));
por=bbValue + ccValue;
aaValue = str2double(get(hObject,'String'));
% etc.
Try implementing the above and see what happens!
1 个评论
Shameer Parmar
2016-6-10
编辑:Shameer Parmar
2016-6-10
Hi Geoff,
I dont think, the line "handles.bb = bb;" is incorrect. In GUI, under function call of respecting buttons or in Opening function of GUI you can make n number of such component of handles. Here 'handle' act as a structure.
Also, once you created such component, you need to save that handles for you GUI object using command "guidata(hObject,handles);" before the ending of that callback.
So it is correct.
Shameer Parmar
2016-6-10
Hi Kh wa,
The problem in your tool is because of line "por=handles.bb+handles.cc;" under aa_callback.
See, when you are typing anything in edit textbox 'aa', it goes into aa_callback function. After coming into aa_callback, it try to calculate 'por' using this command 'por=handles.bb+handles.cc;' but at that time, value of edit textbox bb and cc are not defined so it can not calculate 'por' and it throws an error.
In order to avoid this, better to initialize 'handles.aa', 'handles.bb', 'handles.cc' into opening function of your GUI.
Open your .m file of GUI Goes into xxxxx_OpeningFcn() function and initialize these variables into it.
handles.aa = 0;
handles.bb = 0;
handles.cc = 0;
The another possibility to make it work is,
1. Update the logic in aa_Callback as same as in callback of bb and cc. In callback of bb and cc you are simply checking, whether entered value in number or not. so do the same in callback of bb.
2. Add extra push button into your GUI and into callback of pushbutton add the calculation you doing into callback of aa.
3. into callback of pushbutton, set the value of static text which will display your results.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/174452/image.jpeg)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!