how do i read multiple audio file from different files name ?

8 views (last 30 days)
i have a folder about 136 subfolders which have different names
f1
f2
.
.
.
.
f136
and each subfolder has 10 audio file
these audio files have the same name for whole subfolders
f1 [ sa1 sa2 ....... sa10]
f2 [sa1 sa2 .... sa10]
.
.
f136 [sa1 sa2 ... sa10]
so i need to read them in one matrix
can anyone help me
i will be appreciate
  1 Comment
Stephen23
Stephen23 on 23 Dec 2020
"so i need to read them in one matrix"
Does your computer have enough memory to store all of the imported data in one array? Or for that matter, in separate arrays?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 23 Dec 2020
finfo = dir();
finfo(~[finfo.isdir]) = []; %get rid of non-folders
finfo(ismember({finfo.name}, {'.', '..'})) = []; %get rid of . and ..
foldernames = {finfo.name};
numfolders = length(foldernames);
folderdata = cell(numfolders,3);
for foidx = 1 : numfolders
thisfolder = foldernames{foidx};
dinfo = dir(fullfile(thisfolder, '*.wav'));
thesenames = fullfile(thisfolder, {dinfo.name});
numfile = length(thesenames);
thisdata = cell(numfile,1);
thisfs = zeros(numfile,1);
for fiidx = 1 : numfile
[thisdata{fiidx}, thisfs(fiidx)] = audiread(thesenames{fiidx});
end
folderdata{foidx, 1} = thisdata;
folderdata{foidx, 2} = thisfs;
folderdata{foidx, 3} = thesenames;
end
Output from this is an N x 3 cell array where N is the number of folders. The first column of the cell array, folderdata{K,1} is itself a cell array with one entry per file in the folder that is the audio data. The second column of the cell array, folderdata{K,2} is a vector of sampling frequency data, one for each WAV file in the folder. The third column of the cell array, folder{K,3} is a cell array of file names that were read from in the folder.
This code does not assume that the number of samples is exactly the same for each input file. It also does not assume that the sampling frequency is the same for each file, and it does not assume that the number of channels is the same for each file.
It is not clear to me what your desired output is. You have 136 folders, each with 10 files, number of samples per file is unknown, number of channels per file is unknown. Even assuming that you had exactly the same number of samples per file and that there is only one channel per file, you would still be looking at 136 x 10 x number of samples, but you kind of implied that you expect a 2D array rather than a 3D array.

More Answers (2)

weikang zhao
weikang zhao on 23 Dec 2020
You need a loop body, the loop body continuously generates the path of the audio file.
Run the following script and you will understand.
for i=1:136
for j=1:10
filename=['f',num2str(i),'\sa',num2str(j),'.wav'];
disp(str);
%code to read the audio file %
end
end
have fun!
  4 Comments
rusul a
rusul a on 23 Dec 2020
thank you
but the folders names not f1 to f136 i wrote that as an example the folders have different names like
{'FAEM0';'FAJW0';'FALK0';'FALR0';'FAPB0';'FBAS0';'FBCG1';'FBCH0';'FBJL0';'FBLV0';'FBMH0';'FBMJ0';'FCAG0';'FCAJ0';'FCDR1';'FCEG0';'FCJF0';'FCJS0';'FCKE0';'FCLT0';'FCMG0';'FCMM0';'FCRZ0';'FCYL0';'FDAS1';'FDAW0';'FDFB0';'FDJH0';'FDKN0';'FDML0';'FDMY0';'FDNC0';'FDTD0';'FDXW0';'FEAC0';'FEAR0';'FECD0';'FEEH0';'FEME0';'FETB0';'FEXM0';'FGCS0';'FGDP0';'FGMB0';'FGRW0';'FHLM0';'FHXS0';'FJDM2';'FJEN0';'FJHK0';'FJKL0';'FJLG0';'FJLR0';'FJRB0';'FJRP1';'FJSK0';'FJSP0';'FJWB1';'FJXM0';'FJXP0';'FKAA0';'FKDE0';'FKDW0';'FKFB0';'FKKH0';'FKLC0';'FKLC1';'FKLH0';'FKSR0';'FLAC0';'FLAG0';'FLEH0';'FLET0';'FLHD0';'FLJA0';'FLJD0';'FLJG0';'FLKM0';'FLMA0';'FLMC0';'FLMK0';'FLOD0';'FLTM0';'FMAH1';'FMBG0';'FMEM0';'FMJB0';'FMJF0';'FMJU0';'FMKC0';'FMKF0';'FMMH0';'FMPG0';'FNKL0';'FNTB0';'FPAB1';'FPAC0';'FPAD0';'FPAF0';'FPAZ0';'FPJF0';'FPLS0';'FPMY0';'FREH0';'FRJB0';'FRLL0';'FSAG0';'FSAH0';'FSAK0';'FSBK0';'FSCN0';'FSDC0';'FSDJ0';'FSGF0';'FSJG0';'FSJK1';'FSJS0';'FSJW0';'FSKC0';'FSKL0';'FSKP0';'FSLS0';'FSMA0';'FSMM0';'FSMS1';'FSPM0';'FSRH0';'FSSB0';'FTAJ0';'FTBR0';'FTBW0';'FTLG0';'FTMG0';'FVFB0';'FVKB0';'FVMH0'}
and in each file there is 10 audio file
'SA1.WAV'
'SA2.WAV'
'SI1392.WAV'
'SI2022.WAV'
'SI762.WAV'
'SX132.WAV'
'SX222.WAV'
'SX312.WAV'
'SX402.WAV'
'SX42.WAV'
sorry but im abeginer in matlab
weikang zhao
weikang zhao on 23 Dec 2020
you can get all the names of the subfolder by "dir" function, for example
X=dir;
X will contain information about all subfolders and files in the current folder, X is a struct array, and you can traverse the 'name' field of X.

Sign in to comment.


jibrahim
jibrahim on 23 Dec 2020
If you have access to Audio Toolbox, there is no need for much custom code to accomplish this. Just use audioDatastore.
From the top folder:
ads = audioDatastore(pwd,'IncludeSubfolders',true);
data = readall(ads);
data is a cell array with the audio from all files. If all entries in data have the same dimensions, you can concatenate the signals in a matrix.

Tags

Products


Release

R2015a

Community Treasure Hunt

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

Start Hunting!