Effacer les filtres
Effacer les filtres

Compare .txt to a string.

2 vues (au cours des 30 derniers jours)
Bob Whiley
Bob Whiley le 23 Fév 2015
Commenté : Guillaume le 23 Fév 2015
How can you compare each line of a .txt file to a string?

Réponse acceptée

Joseph Cheng
Joseph Cheng le 23 Fév 2015
you can use the function strcmp(). which will compare two strings.
to expand a bit more you can look up the function fgetl() which has the example
fid=fopen('fgetl.m');
while 1
tline = fgetl(fid);
if ~ischar(tline), break, end
disp(tline)
end
fclose(fid);
and then using strcmp() you'd insert in the for-loop
same = strcmp(tline,'comparisonstrings');
i'll leave it to you to figure out how you want to save/store the results.
  2 commentaires
Bob Whiley
Bob Whiley le 23 Fév 2015
Could you use all instead of strcmp?
Guillaume
Guillaume le 23 Fév 2015
Not if the strings are different length (which will most likely occur). strcmp is the function to compare strings.

Connectez-vous pour commenter.

Plus de réponses (1)

Guillaume
Guillaume le 23 Fév 2015
filelines = strsplit(fileread('sometextfile'), '\n');
issamestring = strcmp(filelines, 'stringtomatch')
  2 commentaires
per isakson
per isakson le 23 Fév 2015
Use strtrim to avoid trailing space of the text file.
Guillaume
Guillaume le 23 Fév 2015
That's assuming you don't want to compare the spaces.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Migrate GUIDE Apps 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