disp('Example: Case Sensitive');
filename = 'Buffaloes.txt';
searchPhrase = 'lying low';
strContents = fileread(filename);
fileAsCellArray = regexp(strContents, '\r\n|\r|\n', 'split');
fileAsCellArray = fileAsCellArray';
idxLineWithSearchPhrase = find(contains(fileAsCellArray,searchPhrase));
if (~isempty(idxLineWithSearchPhrase))
fprintf(1,'In %s, found %d lines with phrase: %s\n',filename,length(idxLineWithSearchPhrase),searchPhrase);
for iline = 1:length(idxLineWithSearchPhrase)
fprintf(1,'Line %d: %s\n',idxLineWithSearchPhrase(iline), ...
fileAsCellArray{idxLineWithSearchPhrase(iline)});
end
else
fprintf(1,'In %s, found no lines with phrase: %s\n',filename,searchPhrase);
end
disp(' ');
disp('Example: Case Insensitive');
filename = 'Buffaloes.txt';
searchPhrase = 'lying low';
strContents = lower(fileread(filename));
fileAsCellArray = regexp(strContents, '\r\n|\r|\n', 'split');
fileAsCellArray = fileAsCellArray';
idxLineWithSearchPhrase = find(contains(fileAsCellArray,searchPhrase));
if (~isempty(idxLineWithSearchPhrase))
fprintf(1,'In %s, found %d lines with phrase: %s\n',filename,length(idxLineWithSearchPhrase),searchPhrase);
for iline = 1:length(idxLineWithSearchPhrase)
fprintf(1,'Line %d: %s\n',idxLineWithSearchPhrase(iline), ...
fileAsCellArray{idxLineWithSearchPhrase(iline)});
end
else
fprintf(1,'In %s, found no lines with phrase: %s\n',filename,searchPhrase);
end
disp(' ');
disp('Example: Case Insensitive - single word (may or may not be plural)');
filename = 'Daffodils.txt';
searchPhrase = 'daffodil ';
strContents = lower(fileread(filename));
fileAsCellArray = regexp(strContents, '\r\n|\r|\n', 'split');
fileAsCellArray = fileAsCellArray';
idxLineWithSearchPhrase = find(contains(fileAsCellArray,searchPhrase));
if (~isempty(idxLineWithSearchPhrase))
fprintf(1,'In %s, found %d lines with phrase: %s\n',filename,length(idxLineWithSearchPhrase),searchPhrase);
for iline = 1:length(idxLineWithSearchPhrase)
fprintf(1,'Line %d: %s\n',idxLineWithSearchPhrase(iline), ...
fileAsCellArray{idxLineWithSearchPhrase(iline)});
end
else
fprintf(1,'In %s, found no lines with phrase: %s\n',filename,searchPhrase);
end
searchPhrase = 'daffodils';
strContents = lower(fileread(filename));
fileAsCellArray = regexp(strContents, '\r\n|\r|\n', 'split');
fileAsCellArray = fileAsCellArray';
idxLineWithSearchPhrase = find(contains(fileAsCellArray,searchPhrase));
if (~isempty(idxLineWithSearchPhrase))
fprintf(1,'In %s, found %d lines with phrase: %s\n',filename,length(idxLineWithSearchPhrase),searchPhrase);
for iline = 1:length(idxLineWithSearchPhrase)
fprintf(1,'Line %d: %s\n',idxLineWithSearchPhrase(iline), ...
fileAsCellArray{idxLineWithSearchPhrase(iline)});
end
else
fprintf(1,'In %s, found no lines with phrase: %s\n',filename,searchPhrase);
end
1 Comment
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/500294-how-to-find-index-of-each-occurrence-of-my-pattern-in-a-file#comment_786109
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/500294-how-to-find-index-of-each-occurrence-of-my-pattern-in-a-file#comment_786109
Sign in to comment.