Comparing two srings using strcmpi

I have stored the data of a uitable into a file dbt9.mat.. The first and second columns conntains the name and IdNo of an individual respectively. When I enter the name the corresponding IDno must be displayed in a editbox..
if true
% code
end
In=load('C:\Users\Animesh\Documents\MATLAB\dbt9.mat');%file name
ln=size(In.comp,1);% comp is the data in uitable dbt9..
for i=1:ln
nam=In.comp{i}
b=strcmpi(nam,name); name is the name I have entered
if b==1
break;
end
end
if b==0
set(handles.edit3,'String','NOT in db');
else
set(handles.edit3,'String',In.comp{ln+i});
end
The problem is that when my gui is opened at first it works perfectly fine but when I try with another name (keeping the gui open) it shows 'NOT in db'..and when i close the gui and open it once again and try with the same name the corresponding Idno is displayed.The problem arises in strcmpi .. Any reason why it is happening like this?? Can anyone help please? Thanks a lot in advance..

 採用された回答

Image Analyst
Image Analyst 2013 年 12 月 26 日

0 投票

Before the strcmpi put these lines:
name
nam
whos name
whos nam
What does it report in the command window? You might also find this useful: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/

8 件のコメント

Animesh
Animesh 2013 年 12 月 26 日
I checked that previously by writing name and nam inside the for loop and checking the results of each iteration which were found to be correct.The problem was with strcmpi .. Now I have put an extra pushbutton for clearing the textboxes.Then it works perfctly fine But I dont understand why was it not working in the previous case though the strings to compare were absolutely correct
Image Analyst
Image Analyst 2013 年 12 月 26 日
Neither will I until you show what I asked you to. Perhaps one was a cell instead of a string.
Animesh
Animesh 2013 年 12 月 26 日
編集済み: Image Analyst 2013 年 12 月 26 日
Sorry Sir ,I thought both were same.. The output in first case is:
Name Size Bytes Class Attributes
name 1x6 12 char global
Name Size Bytes Class Attributes
nam2 1x6 12 char
And in the 2nd case its
Name Size Bytes Class Attributes
name 1x5 10 char global
Name Size Bytes Class Attributes
nam2 1x6 12 char
Image Analyst
Image Analyst 2013 年 12 月 26 日
You did the whos command but not the two lines before that (which have only the variable names all by themselves on the command line with no semicolon) so we don't know what their values (contents) are. nam2 was not mentioned in your original code. Anyway, it looks like in the first case they should match and in the second case they should not match.
Animesh
Animesh 2013 年 12 月 26 日
In the first case When I enter Suraj then the output is: name =
Suraj
nam2 =
Suraj
Name Size Bytes Class Attributes
name 1x6 12 char global
Name Size Bytes Class Attributes
nam2 1x6 12 char
In the second case when I type once again Suraj
name =
Suraj
nam2 =
Suraj
Name Size Bytes Class Attributes
name 1x5 10 char global
Name Size Bytes Class Attributes
nam2 1x6 12 char
nam was modified to nam2 in my code..So the no of bytes of name in the 2nd case does not matches with the one in case 1.
Image Analyst
Image Analyst 2013 年 12 月 26 日
There is probably a white space (like space, linefeed or carriage return) or some other invisible ascii character there. Try using strtrim().
Animesh
Animesh 2013 年 12 月 26 日
It worked..Thanks a lot for helping.
Image Analyst
Image Analyst 2013 年 12 月 26 日
Please mark my answer as Accepted then. Thanks.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeEvent Functions についてさらに検索

質問済み:

2013 年 12 月 26 日

コメント済み:

2013 年 12 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by