How to compare 2 edit text in GUI

3 views (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 16 Jun 2015
if strcmp(get(handles.edit1,'string'), get(handles.edit2,'string'))

More Answers (1)

Image Analyst
Image Analyst on 16 Jun 2015
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.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!