MATLAB Answers

how to find index of each occurrence of my pattern in a file

2 views (last 30 days)
Mekala balaji
Mekala balaji on 14 Jan 2020
Answered: Meg Noah on 14 Jan 2020
Hi,
I have text file (contents are shown belew)
Hi Helo
How are you mekala
what are you doing
where are you going mekala
I want to get the row index if row contains "mekala
I use below code:
fid=fopen("test.txt")
data=fgetl(fid)
idx=strfind(data,'mekala').
idx is showing empty.

  1 Comment

Ruger28
Ruger28 on 14 Jan 2020
fgetl gets a single line. You need to read your entire file in and then try something like you have above.

Sign in to comment.

Answers (1)

Meg Noah
Meg Noah on 14 Jan 2020
This solution will find the lines that contain the whole phrase. The example illustrates how to consider or not consider the case of the letters. It alludes to whether or not a word is plural. Operates on the attached text files. If the phrase is split between two or more lines a different solution would be needed. Is this sufficient for your needs?
%% read the text file
%% find same word phrase in a single line
% a phrase with line-break will not be displayed
disp('Example: Case Sensitive');
filename = 'Buffaloes.txt';
searchPhrase = 'lying low';
strContents = fileread(filename);
fileAsCellArray = regexp(strContents, '\r\n|\r|\n', 'split');
fileAsCellArray = fileAsCellArray';
% find(~cellfun('isempty',contains(fileAsCellArray,searchPhrase)))
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
%% make the above example case insensitive
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';
% find(~cellfun('isempty',contains(fileAsCellArray,searchPhrase)))
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
%% Just a word
disp(' ');
disp('Example: Case Insensitive - single word (may or may not be plural)');
filename = 'Daffodils.txt';
% single
searchPhrase = 'daffodil ';
strContents = lower(fileread(filename));
fileAsCellArray = regexp(strContents, '\r\n|\r|\n', 'split');
fileAsCellArray = fileAsCellArray';
% find(~cellfun('isempty',contains(fileAsCellArray,searchPhrase)))
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
% plural
searchPhrase = 'daffodils';
strContents = lower(fileread(filename));
fileAsCellArray = regexp(strContents, '\r\n|\r|\n', 'split');
fileAsCellArray = fileAsCellArray';
% find(~cellfun('isempty',contains(fileAsCellArray,searchPhrase)))
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

  0 Comments

Sign in to comment.

Tags

Products