Compare .txt to a string.

 采纳的回答

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 个评论

Could you use all instead of strcmp?
Not if the strings are different length (which will most likely occur). strcmp is the function to compare strings.

请先登录,再进行评论。

更多回答(1 个)

filelines = strsplit(fileread('sometextfile'), '\n');
issamestring = strcmp(filelines, 'stringtomatch')

2 个评论

Use strtrim to avoid trailing space of the text file.
That's assuming you don't want to compare the spaces.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by