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!

Réponses (2)

KSSV
KSSV le 1 Fév 2017

0 votes

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
Walter Roberson le 1 Fév 2017
Modifié(e) : Walter Roberson le 1 Fév 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.

Catégories

En savoir plus sur Entering Commands dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by