how to put one dimensional data to two dimensional array?

Hi, there.
I have multiple index text files. (ex, example1.txt, example2.txt, example3.txt, and so on...)
Each text file contains several columns (ex, 10 columns), and among them, I want to extract one specific column (ex, 6th) from each text file and put it into an array in order.
To do that,
I wrote this.
index_start = 1652; index_end = 1653;
for j = index_start:index_end
data(j) = importdata(sprintf('example%d.txt', j));
A = data(j).data(:,6);
end
But, it always returns one dimensional array (A) with the data from 6th column in the last index file (1653).
I believe there must be a solution.
Thanks in advance.

 채택된 답변

Mischa Kim
Mischa Kim 2014년 2월 21일
편집: Mischa Kim 2014년 2월 21일
Lee, try something like
A(:,jj - index_start + 1) = data(j).data(:,6);
This will put the first column that is read into the first column of A, the second column that is read into the second, and so on and so forth.
Just make sure that the column vectors all have the same size. Also, for large matrices A you might want to pre-allocate it, e.g., using the zeros command.

댓글 수: 1

It works!!!
Thanks for the tip of pre-allocation as well!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

질문:

Lee
2014년 2월 21일

댓글:

Lee
2014년 2월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by