strcmp returns false on comparing equal cell

4 vues (au cours des 30 derniers jours)
Dan-Timon
Dan-Timon le 1 Juil 2011
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

Réponse acceptée

Jan
Jan le 1 Juil 2011
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.
  1 commentaire
Dan-Timon
Dan-Timon le 1 Juil 2011
nice, this works. this whole string in a array in a cell in a cell can be really irritating ;)
thanks a lot!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Entering Commands dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by