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

조회 수: 7 (최근 30일)
rusul a
rusul a 2020년 12월 23일
답변: jibrahim 2020년 12월 23일
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
Stephen23
Stephen23 2020년 12월 23일
"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?

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2020년 12월 23일
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.

추가 답변 (2개)

weikang zhao
weikang zhao 2020년 12월 23일
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
rusul a
rusul a 2020년 12월 23일
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 2020년 12월 23일
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.

댓글을 달려면 로그인하십시오.


jibrahim
jibrahim 2020년 12월 23일
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.

카테고리

Help CenterFile Exchange에서 Audio and Video Data에 대해 자세히 알아보기

태그

제품


릴리스

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by