Export many data into the same excel file.

3 views (last 30 days)
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.
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;
m = m + 1;

Accepted Answer

Peng Li
Peng Li on 15 Apr 2020
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.
Peng Li
Peng Li on 15 Apr 2020
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by