strcmp returns false on comparing equal cell
4 次查看(过去 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!