create 3D matrix from input file

1 回表示 (過去 30 日間)
Chappi
Chappi 2015 年 9 月 17 日
コメント済み: Chappi 2015 年 9 月 18 日
Dear experts,
I am very new to Matlab and also don't know much about coding stuff. Would you please help me out?
I have an output file as following
1 1 1 0 372
115 -0.10495324E-08
116 -0.23828453E-07
117 -0.23688876E-07
118 -0.10215027E-08
122 -0.25753166E-12
etc
The first line is meaningless the second line, left column is the index of a 3D matrix, and the right column is its value.
As I know, this file is written out as the radius index varying fastest, the the latitude index and then the longitude index. The index that is not showed in the file is zero.
So how can I create this 3D matrix completely will all zero and non-zero element with correct position of longitude, latitude and radius?
I'm really grateful for your help !
Chappi !
  2 件のコメント
Stephen23
Stephen23 2015 年 9 月 18 日
編集済み: Stephen23 2015 年 9 月 18 日
Your explanation is nice, but it really is much easier and more reliable if you simply upload the file for us to try it out. You can do this by clicking the paperclip button above the text box, and then clicking both Choose file and Attach file* buttons.
Chappi
Chappi 2015 年 9 月 18 日
Thank you so much for your command Stephen. Here is the attached file !
Many thanks, Phung

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

採用された回答

Stephen23
Stephen23 2015 年 9 月 18 日
編集済み: Stephen23 2015 年 9 月 18 日
This code will read the file data into two cell arrays, one containing the header numeric data, the other containing the indices and data. Because this format repeats in four times in the file, the cell arrays have length four.
opt = {'CollectOutput',true};
str = fileread('matrix.txt');
[idb,ide] = regexp(str,'^(\s+\d+){5}\s*$','lineanchors');
idb(end+1) = 1+numel(str);
for k = numel(ide):-1:1
C(k) = textscan(str(idb(k):ide(k)),'%d%d%d%d%d',opt{:});
D(k) = textscan(str(ide()+1:idb(k+1)-1),'%f%f',opt{:});
end
If the first column of data are array indeices then it is very easy to use these to place that data into a 3D array, but you have to tell us the array size.
  1 件のコメント
Chappi
Chappi 2015 年 9 月 18 日
oops.. how silly I was.
As what I wrote before, that this file is written out as the radius index varying fastest, the the latitude index and then the longitude index. The index that is not showed in the file is zero. and the number of grids in longitude, latitude and radius are 41, 41 and 21 respectively.
Thank you so so much Stephen. I will try to understand the script you wrote above.Really appreciate that !
Best, Chappi

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by