フィルターのクリア

Compare .txt to a string.

2 ビュー (過去 30 日間)
Bob Whiley
Bob Whiley 2015 年 2 月 23 日
コメント済み: Guillaume 2015 年 2 月 23 日
How can you compare each line of a .txt file to a string?

採用された回答

Joseph Cheng
Joseph Cheng 2015 年 2 月 23 日
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 件のコメント
Bob Whiley
Bob Whiley 2015 年 2 月 23 日
Could you use all instead of strcmp?
Guillaume
Guillaume 2015 年 2 月 23 日
Not if the strings are different length (which will most likely occur). strcmp is the function to compare strings.

サインインしてコメントする。

その他の回答 (1 件)

Guillaume
Guillaume 2015 年 2 月 23 日
filelines = strsplit(fileread('sometextfile'), '\n');
issamestring = strcmp(filelines, 'stringtomatch')
  2 件のコメント
per isakson
per isakson 2015 年 2 月 23 日
Use strtrim to avoid trailing space of the text file.
Guillaume
Guillaume 2015 年 2 月 23 日
That's assuming you don't want to compare the spaces.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by