fscanf not giving any output
Afficher commentaires plus anciens
Attached below is the current code I have
fid = fopen('sample_velmod.txt','r');
formatspec = '%d %d\n';
sizeA = [2 inf];
A = fscanf(fid,formatspec,sizeA);
fclose(fid);
However, when I run this, I get no output for A. The text file has been implemented into the matlab directory so I know that it is reading the file fine. Any help getting and output for A would be great.
Réponse acceptée
Plus de réponses (1)
Walter Roberson
le 4 Sep 2022
1 vote
fscanf() will return empty under either of two conditions:
- the file is empty; OR
- the data at the file position does not match the first element of the format specification.
For example, the format that you used would fail if there is a header line on the file, unless you read the header line first.
I would suggest that you use readmatrix() as readmatrix() will automatically detect and discard a header line if present.
2 commentaires
Walter Roberson
le 4 Sep 2022
Your file contains, for example,
1100.00 3366.58
Those are not integers.
Also, your file has tab characters in it, and carriage returns. You need
formatspec = '%f\t%f';
Jacob Allen
le 4 Sep 2022
Catégories
En savoir plus sur Low-Level File I/O 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!