Compare .txt to a string.
조회 수: 2 (최근 30일)
이전 댓글 표시
How can you compare each line of a .txt file to a string?
댓글 수: 0
채택된 답변
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
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
2015년 2월 23일
filelines = strsplit(fileread('sometextfile'), '\n');
issamestring = strcmp(filelines, 'stringtomatch')
댓글 수: 2
참고 항목
카테고리
Help Center 및 File Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!