read from multiple text file and store in a 3D array

Hi,
I have 378 text files (0001.txt, 0002.txt, ....0378.txt). I can read each text file using the following code by chaning the file name each time and it works fine. But I want to read all text files using a for loop/using a code and store in a 3D array. I need to store in a 3D array is Eleven1,....upto Eleven378.
Can somebody suggest me a way to do in matlab?
filename = '0001.txt';
delimiterIn = ' ';
headerlinesIn = 1;
Eleven_ST1 = importdata(filename,delimiterIn,headerlinesIn);
% View data
for k = [1:4]
head=(Eleven_ST1.textdata{1});
Eleven1=(Eleven_ST1.data(:, k));
Eleven1_YYRR=([Eleven_ST1.data(:,1) Eleven_ST1.data(:,4)]);
end
Thanks in advance.

回答 (1 件)

dpb
dpb 2014 年 6 月 17 日

1 投票

See the FAQ
I strongly recommend the dir solution.
If the data in each file are 2D, then preallocate the number of planes based on the result of dir (instead of hardcoding in 378 to make the script more generally useful) and then store in the plane as
data(:,:,ix)=[file_data1 file_data2];
where ix will be the loop over 1:length(d) and
d=dir('*.txt'); % the files to process directory structure

6 件のコメント

Damith
Damith 2014 年 6 月 17 日
編集済み: Damith 2014 年 6 月 17 日
Thanks. My above code is working fine to read one file at a time. But how can I modify the above code to read 378 text files and store in a 3D array?
If somebody can suggest me a way to dynamically change the filename (i.e. 0001 to 0002 so on) and store in a (:,:,:) that would be great.
dpb
dpb 2014 年 6 月 17 日
Read the FAQ; examples are given...I strongly recommend using the dir solution.
Damith
Damith 2014 年 6 月 17 日
Thanks. I got it worked.
dpb
dpb 2014 年 6 月 17 日
See, now don't you feel like you accomplished something and learned a little more on the way???? VBG
Damith
Damith 2014 年 6 月 17 日
Yes. Thanks for your guidance though.
dpb
dpb 2014 年 6 月 17 日
You're welcome...my intent is generally aimed to "teach to fish" as well as get a solution...

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

カテゴリ

ヘルプ センター および File ExchangeFile Operations についてさらに検索

質問済み:

2014 年 6 月 17 日

コメント済み:

dpb
2014 年 6 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by