Problem loading .wav files in matlab from directory

  • Thread starter Thread starter Jamshaid3
  • Start date Start date
  • Tags Tags
    files Matlab
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 3K views
Jamshaid3
Messages
2
Reaction score
0
Hello participants. Can anyone help me in the code of loading wavs... I am getting some error...
Function for the Code is...


function [wavs,fs] = load_wavs(directory)

% Initialize variables
data = 0;
fs = 0;

% Load the wavs
if(nargin < 1),
D = dir;
else
D = dir(directory);
end
for i=2 : size(D,1),
if(size(findstr(D(i).name,'.wav'),1))
[wav,fs,nbits] = wavread(D(i).name);
wav = wav ( :,1 );
data = add_wav(wav,data);
end
end
wavs = zeros(size(data,2)-1,size(data,1));
for i=2 : size(data,2),
wavs( i-1,: ) = data ( :,i )';
end

After saving it when I call it from command window ...

x = load_wavs('test') %test is a folder containing .wav files in my current directory

It gives me error...

? Error using ==> wavread at 67
Cannot open file.

Error in ==> load_wavs at 26
[wav,fs,nbits] = wavread(D(i).name);

Am I doing something wrong... I have tried to change the path but same result...
Please help me to get rid of this inconvenience... ASAP
 
Physics news on Phys.org
Welcome to PF!

It helps a lot if you bracket your MATLAB code with [ code] tags. It will preserve the indentation and spacing
of the source and offset it from the rest of your post.

With respect to the .wav file, it may depend on the version of MATLAB you have but here's for the latest version:

http://www.mathworks.com/help/matlab/ref/wavread.html

and here's some discussion on reading a wav file from the current directory:

http://stackoverflow.com/questions/6319870/how-to-read-a-wav-file-into-matlab[/code]
 
Last edited by a moderator:
I am using MATLAB R2009a. I have tried many ways but all in vane... Could you please write down some code to load multiple .wav files from a directory...
 
Jamshaid3 said:
I am using MATLAB R2009a. I have tried many ways but all in vane... Could you please write down some code to load multiple .wav files from a directory...

Did you read the references in my post? I'm not near a MATLAB implementation and I haven't had a need to work with wav files. The references look pretty good. You can compare your code to what you see and maybe you'll find what is wrong.

Can find a function that will tell you what MATLAB thinks is the current directory?

And print the current directory path.

It may be that your files are not in the directory that MATLAB is looking at.