How to use fscanf in matlab to read any element of a matrix?
Show older comments
f1=fopen('input.dat');
data1=fscanf(f1,'%d',[2,3]);
data1'
this is not giving desired results. the input matrix is
1 2 3
4 5 6
7 8 9
3 Comments
Stephen23
on 6 Aug 2015
And what are the "desired results"?
shilu
on 7 Aug 2015
dpb
on 7 Aug 2015
With a sequential ASCII file, only possible by an extension of what I showed earlier of skipping over N elements where the N is computed from the field position to the linear position in sequence in the file, or if it is a fixed-width file one can equivalently compute the byte offset (remembering to include the \n character(s) in the computation) and position in the file via fseek.
All in all, as I suggested in the previous response, you're generally far better off to simply read the full array into memory and retrieve the desired locations via sub2ind.
NB: the computation of position in memory is column-major whereas in a file you're reading by record--for example if the file were three records by two columns, your (2,2) position above would be the fifth element in memory but the fourth in the file.
Accepted Answer
More Answers (0)
Categories
Find more on Large Files and Big Data 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!