Problem loading .wav files in matlab from directory

  • Thread starter Thread starter Jamshaid3
  • Start date Start date
  • Tags Tags
    files Matlab
AI Thread Summary
The discussion centers on issues with loading .wav files in MATLAB, specifically using a custom function to read files from a directory. The user encounters an error stating "Cannot open file" when attempting to load .wav files, despite confirming the path. Suggestions include checking the current directory in MATLAB and ensuring that the .wav files are located in the specified directory. References to MATLAB documentation and external resources are provided for further assistance. The user requests a working code example for loading multiple .wav files, indicating ongoing difficulties with the task.
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.
 

Similar threads

Replies
15
Views
7K
Replies
1
Views
5K
Replies
3
Views
1K
Replies
1
Views
3K
Replies
8
Views
2K
Replies
2
Views
5K
Replies
4
Views
1K
Replies
1
Views
2K
Back
Top