How to use fscanf in matlab to read any element of a matrix?
古いコメントを表示
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 件のコメント
Stephen23
2015 年 8 月 6 日
And what are the "desired results"?
shilu
2015 年 8 月 7 日
dpb
2015 年 8 月 7 日
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.
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Large Files and Big Data についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!