Add 2 numbers in MATLAB, answer to appear in edit text box moment, the numbers are entered in 2 text boxes, without using pushbutton

I am trying to get value in edit3 textbox without using pushbutton
function edit3_Callback(hObject, eventdata, handles)
% hObject handle to edit3 (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 edit3 as text
% str2double(get(hObject,'String')) returns contents of edit3 as a double
a = str2num(get(handles.edit1,'String'));
b = str2num(get(handles.edit2,'String'));
c = a + b;
set(handles.edit3,'String',num2str(c));

 채택된 답변

function edit2_Callback(hObject, eventdata, handles)
a = str2double(get(handles.edit1,'String'));
b = str2double(get(handles.edit2,'String'));
if isnan(a) || isnan(b) %empty or not number
return;
end
c = a + b;
set(handles.edit3,'String',num2str(c));
end
This callback will be invoked without needing a pushbutton. It does, however, require that the user presses Return in the edit box, or that the user clicks outside the edit box after having entered text in the box.

댓글 수: 8

You could also do it the OOP way:
a = str2double(handles.edit1.String);
b = str2double(handles.edit2.String);
c = a + b;
handles.edit3.String = num2str(c);
If you want it to compute the moment you type anything in one of the edit fields, I think you might need to set up a timer to periodically get the values every fifth of a second or so, sum them, and send the result to edit3.
This is "traditional figures". If you fetch the String property of a uicontrol style edit while someone is typing in it, you will not see the updates -- not until they press return or focus on another object. You would have to go in at the Java level in order to get the current String property as someone is typing. Or hook into the KeyPressFcn callback and keep track of everything yourself including correcting for delete keys.
Thank you.
if isnan(a) || isnan(b) %empty or not number
return;
end
If one of the cells is left empty the solution cell still dispalys the same ans (as was in previous step). How to ensure that it updates its value relative to the cell which has number or throws some error message.
a = str2double(handles.edit1.String);
b = str2double(handles.edit2.String);
c = a + b;
handles.edit3.String = num2str(c);
The answer does comes NaN when one cell is empty. If i want to pop out, please enter the value of "something".
if isnan(a)
warningMessage = sprintf('a is empty.\nPlease put something in the edit field for a.');
uiwait(errordlg(warningMessage));
handles.edit3.String = 'Please enter a'
return;
end
Same for b.
if isnan(a) || isnan(b) %empty or not number
handles.edit3.String = 'Inputs are not valid yet';
return;
end

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 App Building에 대해 자세히 알아보기

질문:

2021년 3월 14일

댓글:

2021년 3월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by