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
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 个评论
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!