How to Cut multiple files from multiple folders in a for loop? Then return them to their own folders containing those cuts.

2 次查看(过去 30 天)
I have this working section that I have run all my other m files for, but i can only do it for the 12 2sec cuts of the 1st wav file under the folder directory named basson.....the Basson folder has 10 total wav files in it though.
I would like to get this for loop to work through all of the main 4 folders of wav files and slit them all up into 2sec clips,? any suggestions would help greatly!
got to have all this cut up so i can compare each data set later in program......
only other way i can think of is hard coding it, but it was going upwards of 800+ lines! ha
that line gives me files like, 3Basson1.wav, 3Basson2.wav........to 12
hopefully i worded this correctly....
Thanks again
file_name=sprintf('%dBassoon%d.wav',3,i) %can this be changed to %d%dBassoon%d.wav, "something to increment them?",i)
%----------------------------------------------------
%what i have so far working for the very first of 10 wav files in that folder
filename='C:\Users\......\Bassoon\01_AchGottundHerr_bassoon.RESYN.wav'; %does this need to be a dir?
[y, fs] = audioread(filename);
k=ceil(length(y)/(2*fs));
mkdir Cut_Bassoon
% mkdir Cut_Clarinet
% mkdir Cut_Saxphone
% mkdir Cut_Violin
s=2*fs;
n = length(y);
for i = 1: (k-1)
file_name=sprintf('%dBassoon%d.wav',3,i);
file_name=strcat('C:\Users\.......\Cut_Bassoon\', file_name);
audiowrite(file_name,y((s*(i-1)+1): (s*i)),fs); % this does the actual segmenting into 2sec and sends to that dir
end

采纳的回答

Paul Hoffrichter
Paul Hoffrichter 2020-12-4
You can recursively find all files with a .wav extension using code from here:
But without the metadata in some of the .wav files that you create, it appears that you have to handle separately the first .wav file that has the metadata from the rest that do not.
  4 个评论
morgan mcvay
morgan mcvay 2020-12-7
编辑:morgan mcvay 2020-12-7
Thankyou, over the past few days I have greatly increased my MATLAB performance, and i ended up taking some of the suggestions from the example code link you provided, I ultimetly used a dir search and threw then resorted them into new folder using 4 for loops.......not the most efficient! but it worked! haha
Point being, i wouldnt have figured it out without your link, so thank you.
filenameB=dir('C:\Users...........\*.wav');
filenameC=dir('C:\Users\........\*.wav');
filenameS=dir('C:\Users.........\*.wav');
filenameV=dir('C:\Users\..........*.wav');
mkdir Cut_Bassoon
mkdir Cut_Clarinet
mkdir Cut_Saxphone
mkdir Cut_Violin
for c = 1:numel(filenameC)
Name = filenameC(c).name;
[y, fs] = audioread(Name);
k=ceil(length(y)/(2*fs));
s=2*fs;
t = 1;
for i = 1: (k-1)
file_name = ['Clarinet_CUT' num2str(t) '_',Name];
audiowrite(file_name,y((s*(i-1)+1): (s*i)),fs);
t = t + 1;
end
end
% and repeted this 3 more times for each file.........

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import and Analysis 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by