How do I stack many files together into one?

조회 수: 4 (최근 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.

답변 (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