Add 2 numbers in MATLAB, answer to appear in edit text box moment, the numbers are entered in 2 text boxes, without using pushbutton
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Avinav Kumar
am 14 Mär. 2021
Kommentiert: Avinav Kumar
am 14 Mär. 2021
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));
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 14 Mär. 2021
Bearbeitet: Walter Roberson
am 14 Mär. 2021
Create the same Callback property for both edit boxes https://www.mathworks.com/help/matlab/ref/matlab.ui.control.uicontrol-properties.html#bt6ck7c-1-Callback
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 Kommentare
Walter Roberson
am 14 Mär. 2021
if isnan(a) || isnan(b) %empty or not number
handles.edit3.String = 'Inputs are not valid yet';
return;
end
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!