Compare .txt to a string.
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Bob Whiley
am 23 Feb. 2015
Kommentiert: Guillaume
am 23 Feb. 2015
How can you compare each line of a .txt file to a string?
0 Kommentare
Akzeptierte Antwort
Joseph Cheng
am 23 Feb. 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 Kommentare
Guillaume
am 23 Feb. 2015
Not if the strings are different length (which will most likely occur). strcmp is the function to compare strings.
Weitere Antworten (1)
Guillaume
am 23 Feb. 2015
filelines = strsplit(fileread('sometextfile'), '\n');
issamestring = strcmp(filelines, 'stringtomatch')
2 Kommentare
Siehe auch
Kategorien
Mehr zu String Parsing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!