How do I stack many files together into one?

2 ビュー (過去 30 日間)
Jason
Jason 2014 年 6 月 4 日
コメント済み: Mahdi 2014 年 6 月 4 日
I have a large number of data runs saved in .dat files. Each data set may consist of up to 1000 separate files (each run has a file). I wish to stack them all together in one large matrix. What would the best way to do this? I can do it by hand but I would like to automate it as I have many sets to compile. The saved files have a final tag of spectraA1.dat, spectraA2.dat, etc. I want to compile into a master file say spectraA.dat. Does this make sense? All matrix in files are the same dimension.
  1 件のコメント
José-Luis
José-Luis 2014 年 6 月 4 日
Depends on what's inside your file.

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

回答 (2 件)

Mahdi
Mahdi 2014 年 6 月 4 日
One way of doing this
name='SpectraA'
for i=1:5 #Let's say you have SpectraA1....SpectraA5
Filename=strcat(name, num2str(i) , '.dat')
x(:,i)=load(Filename); # If it's a vector with one column
end
If the data is in a vector in one row, use:
x(i,:)=.....
instead
If they all have the same number of columns, you can do the following ( warning : not recommended to do it this way though, there are better ways):
name='SpectraA'
x=[];
for i=1:5 #Let's say you have SpectraA1....SpectraA5
Filename=strcat(name, num2str(i) , '.dat')
data1=load(Filename); # If it's a vector with one column
x=[x; data1];
end
  2 件のコメント
Jason
Jason 2014 年 6 月 4 日
data is saved as a row vector (with roughly 1000 entries).
Thanks for the help ! I'm just learning Matlab.
Mahdi
Mahdi 2014 年 6 月 4 日
No problem. If the issue is resolved, please accept an answer.

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


Joseph Cheng
Joseph Cheng 2014 年 6 月 4 日
as you can do it by hand (i'm assuming you're manually opening and appending to the final file (fopen->fwrite at end of file->repeat) you can formulate that into a for loop. with that loop you can use the dir function to get all the *.dat files in a folder.
dir(fullfile(folder,('*.dat'))
this will get all the dat files hopefully in the order you want then use that list in your for loop.

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by