How to compare 2 edit text in GUI

조회 수: 2 (최근 30일)
Adam Taher
Adam Taher 2015년 6월 16일
답변: Image Analyst 2015년 6월 16일
Hello,
I have a GUI with 2 edit text and 1 pushbutton. What I would like to do is to set a value to 1 edittext (say edit2) and within a while loop, increment the value of the other edittext (say edit1). For example: edit2 is set to value = 2, and when edit1's value = 2, then display('ok')
Here is the code:
*function edit2_Callback(hObject, eventdata, handles)*
% hObject handle to edit2 (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 edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
handles.edit2 = get(hObject, 'String');
display(handles.edit2);
*function pushbutton1_Callback(hObject, eventdata, handles)*
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles = guidata(hObject);
handles.burst = 0;
while handles.burst ~= 10
handles.burst = handles.burst+ 1;
pause(1);
set(handles.edit1, 'String', sprintf('%d', handles.burst));
if strcmp(handles.edit1, handles.edit2)
display('ok');
end
% Update handles structure
guidata(hObject, handles);
end

채택된 답변

Walter Roberson
Walter Roberson 2015년 6월 16일
if strcmp(get(handles.edit1,'string'), get(handles.edit2,'string'))

추가 답변 (1개)

Image Analyst
Image Analyst 2015년 6월 16일
No, that is bad. Just look at the line in the callback for edit2:
handles.edit2 = get(hObject, 'String');
You're overwriting the tag with the contents - whatever the user happened to type in. And your other callback is strange too. I don't know what the loop over burst is, why burst needs to be a field of handles, why you overwrite the tags of the edit fields instead of retrieving the string contents, and why you're using strcmp() improperly by comparing tags (that is, if you haven't already destroyed the tags). To do a comparison, you need to do
string1 = get(handles.edit1, 'String');
string2 = get(handles.edit2, 'String');
if strcmp(string1, string2)
fprintf('String 1 matches string 2.\n');
end
DO NOT, EVER set handles.edit1 equal to anything.

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by