Using audioread to read a sequence of files and then combine

10 views (last 30 days)
I am am trying to read all wav files within a folder and then combine those files. I understannd audioread and audiowrite. I am stuggling to automate the processing of a sequence of files within a folder so that I only have to change the dirin name and can move to the next folder. The code here obviously doesn't work but trying to demonstrate what I want to do.
DirIn = 'C:\Users\24hr sound analysis\17';
eval(['filelist=dir(''' DirIn '/*.wav'')'])
for i = 1:length(filelist);
[f(i),fs] = audioread(strcat(DirIn,'/',filelist(i).name)); % read in wav file
end
combined = [f1;f(i)];
audiowrite('combined.wav',combined, fs)

Answers (1)

Stephen23
Stephen23 on 28 Jun 2021
Edited: Stephen23 on 28 Jun 2021
Do NOT use EVAL for trivial code like this.
Use FULLFILE instead of concatenating text together.
P = 'C:\Users\24hr sound analysis\17';
S = dir(fullfile(P,'*.wav'));
for k = 1:numel(S)
F = fullfile(P,S(k).name);
[S(k).data,fs] = audioread(F);
end
M = vertcat(S.data); % comma-separated list
audiowrite('combined.wav', M, fs)

Community Treasure Hunt

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

Start Hunting!