Export many data into the same excel file.
2 次查看(过去 30 天)
显示 更早的评论
Hello Community,
I have about a thousand audio files in this directory and I want to export them to the same excel, without overwriting the previous one.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/284659/image.png)
I have this code, is anyone able to improve it? Or another solution? Thank you in advance!
samp_rate = 44000;
m = 1;
cd 'some folder'
Extracted_files = dir;
nExtracted_files = length(Extracted_files);
filename = 'reference.xlsx';
for i = 1 : nExtracted_files
[data,samp_rate] = audioread(Extracted_files(i).name);
A(m) = data;
save([Extracted_files(i).name)
m = m + 1;
end
0 个评论
采纳的回答
Peng Li
2020-4-15
for i = 1 : nExtracted_files
[data,samp_rate] = audioread(Extracted_files(i).name);
% different files may have different length, so the following sentence
% might give you an error
% A(m) = data;
% And if they are with the same length, better way is to preallocate
% matrix A to speed up
% this only saves to mat
% save([Extracted_files(i).name)
% you don't need this m, as you have the loop variable i
% m = m + 1;
% below will export the data to a column of the spreadsheet starting
% from A, and then B, and so on.
writematrix(data(:), 'youExcelFileName', ...
'FileType', 'spreadsheet', 'Sheet', 1, 'Range', [char('A'+(i-1)) '1']);
% note, you may need to change the value for Range
% [char('A'+(i-1)) '1'] if you have more than 26 files. This only works
% if you have less than 26 files.
end
2 个评论
Peng Li
2020-4-15
This won't be working; you'd need to work out a better way to define Range properties as [char('A'+(i-1)) '1'] goes to Z1 after 26th file and would be [1 which doesn't make sense for Excel.
I think the first thing you need to think is do you necessarily need to write them to Excel. They have different length, and after that you are up to a weird Excel file which is difficult to handle. What you will have to do is probably to load these columns one by one, which is equivalent to what you have done with audioread.
If you want to extract an equal length of segment from each of these audio files, you can concatenate these segments into an array and write the array to file together once, which is more efficient and you don't need to worry about the Range stuff in Excel.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!