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

52 views (last 30 days)
i have a folder about 136 subfolders which have different names
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 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({}, {'.', '..'})) = []; %get rid of . and ..
foldernames = {};
numfolders = length(foldernames);
folderdata = cell(numfolders,3);
for foidx = 1 : numfolders
thisfolder = foldernames{foidx};
dinfo = dir(fullfile(thisfolder, '*.wav'));
thesenames = fullfile(thisfolder, {});
numfile = length(thesenames);
thisdata = cell(numfile,1);
thisfs = zeros(numfile,1);
for fiidx = 1 : numfile
[thisdata{fiidx}, thisfs(fiidx)] = audiread(thesenames{fiidx});
folderdata{foidx, 1} = thisdata;
folderdata{foidx, 2} = thisfs;
folderdata{foidx, 3} = thesenames;
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
%code to read the audio file %
have fun!
weikang zhao
weikang zhao on 23 Dec 2020
you can get all the names of the subfolder by "dir" function, for example
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 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.





Community Treasure Hunt

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

Start Hunting!

Translated by