How do I stack many files together into one?

2 次查看(过去 30 天)
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.

回答(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 CenterFile Exchange 中查找有关 File Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by