フィルターのクリア

How to read data from txt file and to create a 2D matrix?

2 ビュー (過去 30 日間)
Novice Geek
Novice Geek 2014 年 2 月 13 日
コメント済み: Novice Geek 2014 年 2 月 13 日
I am writing a function which reads a text file with some header information in the first 10 lines and then the actual data starts. Here is how the file looks:
Line 1 to
.....
Line 10 (header information)
0 0 4.169080e+000
1 0 6.391669e+000
2 0 6.391669e+000
.
.
.
511 0 9.922547e+000
0 1 5.268026e+000
1 1 5.268026e+000
.
.
.
511 511 9.922547e+000
I have extracted information from the lines which are part of the header. Line 11 onwards the line format is:
x y value
I want to read all these lines one by one (line 11 onwards) and form a 2D matrix(dimensions: 511 X 511) of the value column so that later I can generate an image using this (basically using squeeze() and imshow() functions). Can someone help me how I organize this in a matrix? I am trying to use a yLoop and a nested xLoop but it is not working.

採用された回答

Andrei Bobrov
Andrei Bobrov 2014 年 2 月 13 日
編集済み: Andrei Bobrov 2014 年 2 月 13 日
f = fopen('yourtextfile.txt');
c = textscan(f,'%d %d %f','HeaderLines',10,'CollectOutput',1);
fclose(f);
out = accumarray(c{1}+1,c{2});
  3 件のコメント
Andrei Bobrov
Andrei Bobrov 2014 年 2 月 13 日
Sorry, my typo, corrected.
Novice Geek
Novice Geek 2014 年 2 月 13 日
It works now ! :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeRead, Write, and Modify Image についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by