How do I stack many files together into one?

2 visualizaciones (últimos 30 días)
Jason
Jason el 4 de Jun. de 2014
Comentada: Mahdi el 4 de Jun. de 2014
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.

Respuestas (2)

Mahdi
Mahdi el 4 de Jun. de 2014
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 comentarios
Jason
Jason el 4 de Jun. de 2014
data is saved as a row vector (with roughly 1000 entries).
Thanks for the help ! I'm just learning Matlab.
Mahdi
Mahdi el 4 de Jun. de 2014
No problem. If the issue is resolved, please accept an answer.

Iniciar sesión para comentar.


Joseph Cheng
Joseph Cheng el 4 de Jun. de 2014
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.

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by