search for specific word in text file in Matlab

Status
Not open for further replies.

Reem_Abdullah

Newbie level 4
Joined
Dec 12, 2011
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,319
I want to search for specific word in text file and return its position ...

this code read the text fine...

Code:
fid = fopen('jojo-1 .txt','r');

while 1

tline = fgetl(fid);

if ~ischar(tline)

break

end

end

but when I add this code
Code:
U = strfind(tline, 'Term');

it return [] although the string 'Term' exist in the file

can you please help me
 

Try this code on text.txt file which is attached. You get 13 and 1 which are the location of Term in 2nd and 3rd lines of text.txt

%%%%%%%%%%%%%%%%%%%%%%%
clc;
fid = fopen('text.txt','r');

tline = fgetl(fid)
while ischar(tline)
line_string = sprintf('%s',tline);
u = strfind(line_string, 'Term')
tline = fgetl(fid); %go to next line
end

fclose(fid);
%%%%%%%%%%%%%%%%%%%%%%%%%
 

Attachments

  • text.txt
    47 bytes · Views: 44

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…