weird issue with fgetl
Show older comments
Hello,
I have a text document called 'num.dat' that contains:
969592914
969592915
969592916
969592917
when I execute the commands:
fid = fopen('num.dat','rt');
fgetl(fid)
fgetl(fid)
I get the output:
ans =
'969592914'
ans =
''
Instead of (as I expected):
ans =
'969592914'
ans =
'969592915'
And I can't figure out why, thanks in advance!
Answers (2)
KSSV
on 1 Feb 2017
That is not weird. You should know that the second line is empty so the output is ''. To get your expected behavior remove the blank spaces. Or alternatively you may try:
clc; clear all ;
fid = fopen('num.dat','rt');
l1 = fgetl(fid)
while ~isa(l1,'double')
l1 = fgetl(fid)
end
fclose(fid) ;
Walter Roberson
on 1 Feb 2017
Edited: Walter Roberson
on 1 Feb 2017
0 votes
You posted here with a format that suggests there are blank lines in the input. If so then the second fgetl() is going to retrieve the blank line after the first line. fgetl() does not skip empty lines.
If this does not seem to be the issue, please attach a copy of a file that it happens for.
Categories
Find more on Entering Commands in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!