strcmp returns false on comparing equal cell
조회 수: 5 (최근 30일)
이전 댓글 표시
Hello, I'm writing an analyzer for logfiles of an filetransferprogram. Therefore I'm reading the logfile line by line, extract the usernick and compare it to a cellarray of usernicks. If the nick is already somewhere in the cellarray it increases a counter for this nick, otherwise the nick is written at the end of the cellarray:
nicks = cell(1);
nicks{1,1}{1}=[];
line = textscan(ulfile,'%s',1,'delimiter','\n'); %reading one line
ul = strfind(line{1,1}{1}, 'uploaded to'); %find place of usernick
tempnick = textscan(line{1,1}{1}(ul:end), '%*s %*s %s',1); %extract usernick
for k=1:length(nicks)
if strcmp(nicks{1,k}{1},tempnick{1,1})
match = k;
else
match = 0;
end
end
This code is iterated over every line within the logfile. The problem now is, that sometimes the strcmp works and sometimes not. In the nicks-cellarray is at the end for example two times the user '[abc]crusso', one time with the counter on 3 and one time with the counter on 5. I even tried it with a 39-digit hash of the usernicks without any result. When I'm comparing the strings afterwards manually, strcmp mostly returns 1 on the same usernicks. I'm absolutely clueless, where the problem might be. Thanks for help in advance. Dante
댓글 수: 0
채택된 답변
Jan
2011년 7월 1일
You did not create a cell string, but a scalar cell, which contains a cell string:
nicks = cell(1);
nicks{1,1}{1}=[]; % Now nicks is {{[]}}
Therefore "length(nicks)" is always 1.
I assume this will be better:
nicks = {};
DataC = textscan(ulfile,'%s',1,'delimiter','\n');
Line1 = dataC{1}{1}; % first line as string
ul = strfind(Line1, 'uploaded to'); %find place of usernick
tempnickC = textscan(Line1(ul:end), '%*s %*s %s',1);
tempnick = tempnickC{1}{1};
k = find(strcmp(tempnick, nicks));
if isempty(k)
nicks{end + 1} = tempnick;
end
Note: I avoid the name "line", because this is a built-in function.
I think TEXTSCAN is too complicated for the frequent and simple standard task of geting a text file as cell string. It is really sad that the easier DATAREAD (called from the deprecated TEXTREAD and STRREAD) is not supported anymore.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Entering Commands에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!