How to compare 2 edit text in GUI
1 回表示 (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
Walter Roberson
2015 年 6 月 16 日
if strcmp(get(handles.edit1,'string'), get(handles.edit2,'string'))
0 件のコメント
その他の回答 (1 件)
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!