Help needed rearranging data into a matrix

2 ビュー (過去 30 日間)
Michael
Michael 2013 年 5 月 6 日
I am importing data from a txt file that has a column list of values. I would like to populate a matrix with these values in a looped way so that the matrix fills each column in a row with consecutive values from the txt list, then moves to the next row to fill each column with the next set of values, and so on. I need this to work with any number of values and matrix size, so that if there are a total of m*n values in the column list, the matrix produced will be of size m x n.
The data is read from a txt file comprising 4 columns of information, of which I am only interested in the 4th. The order of population is critical as two of the list columns give x & y spatial coordinates that are associated with each of the values in the 4th column.
Currently my code looks like this:
W = importdata('307_6_5.txt',' ',8);
hor_el = 6;
ver_el = 5;
MODE = zeros(ver_el+1, hor_el+1);
for k = 1:(hor_el+1)*(ver_el+1)
for i = 1:ver_el + 1;
for j = 1:hor_el + 1;
MODE(i,j) = W.data(k, 4);
end
end
end
I know this will be embarrassingly simple for someone who knows what they are doing but unfortunately my Matlab skills are still very poor. Any help would be much appreciated.
Thankyou.
  2 件のコメント
Babak
Babak 2013 年 5 月 6 日
can you give an example of how the .txt file and how the corresponding matrix look like? Did you try textscan()?
Michael
Michael 2013 年 5 月 6 日
Thanks for your response. Underneath the 8 header lines, the text file is 4 columns of 42 lines of data. This is a link to the .txt file: http://www.4shared.com/office/DIHP0dJh/307_6_5.html
I'm trying to get the program to be able to deal with similar files comprising many more lines though, but just wanted to work on something very simple first.
The matrix is intended to represent the values from the 4th column of the text file over xy coordinates, but the actual values of the coordinates (stored in columns 1 and 2) are not so important here as these will be defined when the matrix is plotted over a colour mesh.
I have glanced at the help for textscan but couldn't see that it offered anything to suggest using it would be better than importdata - the file is reading in ok as a struct object; it's just a case of needing to arrange into a slightly different form I think. When I searched for skipping header lines of text files the importdata function seemed the most straightforward - is there something that textscan can do that will help here?
Many thanks
Mike

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

採用された回答

Dr. Seis
Dr. Seis 2013 年 5 月 6 日
編集済み: Dr. Seis 2013 年 5 月 6 日
You appear to be overwriting MODE for each k such that, at the end of the for loops, each element in MODE is equal to the last data value. What you may need to do is to create something for determining the row/column indices associated with each k. For example:
W = importdata('307_6_5.txt',' ',8);
hor_el = 6;
ver_el = 5;
rowIDX = 1;
colIDX = 1;
MODE = zeros(ver_el+1, hor_el+1);
for k = 1:(hor_el+1)*(ver_el+1)
MODE(rowIDX,colIDX) = W.data(k, 4);
colIDX = colIDX+1;
if colIDX > (hor_el + 1)
rowIDX = rowIDX+1;
colIDX = 1;
end
end
If you understand the way that works, then you should be able to replace all that with something that has the same effect but with less typing:
W = importdata('307_6_5.txt',' ',8);
hor_el = 6;
ver_el = 5;
MODE = reshape(W.data(:,4),hor_el+1,ver_el+1)';
  1 件のコメント
Michael
Michael 2013 年 5 月 6 日
That's really helpful, thankyou. Very useful to see how the structure should work but thanks also for highlighting the reshape function.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by