How do I read all the wav files in a directory from a single loop ?

3 次查看(过去 30 天)
I have Emo-DB databse containing file name wav files with file name as following
1   03a01Ab.wav    
2   03a01Eb.wav   
3   03a01Fa.wav    
4   03a01Lb.wav    
5   03a01Nc.wav      
6   03a01Tc.wav     
7   03a01Wa.wav    
8   08a01Aa.wav     
9   08a01Ab.wav     
10   08a01Ec.wav  
11   08a01Fd.wav   
12   08a01Lc.wav    
13   08a01Na.wav     
14   08a01Td.wav   
15   08a01Wa.wav
16   08a01Wc.wav    
17   09a01Aa.wav  
18   09a01Ea.wav   
19   09a01Fa.wav      
20   09a01Lc.wav     
21   09a01Nb.wav
Here    
  • # Positions 1-2: number of speaker
  • # Positions 3-5: code for textspoken
  • # Position 6: emotion
  • # Position 7: if there are more than two versions these are numbered a, b, c ....
Now how do I read all these files using using audioread in a single loop. I tried the following code but it doesn't work:
for i=0
for j=0:0
for k=0:0
fid = fopen(filename,'a');
file =sprintf('%s%d%s%d%d.wav', 'D:\BTP\0 (1)\', i,'\',j,k);
[s, fs] = audioread(file);
end
end
end

回答(2 个)

Ameer Hamza
Ameer Hamza 2018-4-21

Instead of trying to create file names yourself, uou can use dir command to get list of all files in the current folder. Then use the list to read all files with an extension of '.wav'.

files = dir;
count = 0;
for i = files'
    if length(i.name) > 4 && i.name(end-3:end) == ".wav"   % only read file if filename is greater then four and extension is '.wav'
        count = count+1;
        [f{count} fs{count}] = audioread(i.name); 
    end
end

This script will load all data in cell arrays f and fs.

  2 个评论
Jan
Jan 2018-4-21

Or easier:

FileList = dir('D:\BTP\0 (1)\*.wav');
for File = FileList'
   count                 = count + 1;
   [f{count}, fs{count}] = audioread(File.name); 
end

请先登录,再进行评论。


Dinesh Iyer
Dinesh Iyer 2018-4-27
You can use the fileDatastore with the audioread as the Custom function.
https://www.mathworks.com/help/matlab/ref/matlab.io.datastore.filedatastore.html

Community Treasure Hunt

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

Start Hunting!

Translated by