Problem loading .wav files in matlab from directory

In summary: Or maybe they're not readable by whatever program you're using.In summary, the conversation discusses an issue with loading .wav files in MATLAB. The function for loading the files is provided, along with the code used to call it. However, the code is resulting in an error and the asker is seeking help to resolve the issue. They have tried various solutions but have not been successful.
  • #1
Jamshaid3
2
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
  • #2
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:
  • #3
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...
 
  • #4
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.
 
  • #5


Hello,

Thank you for reaching out for help with your code. It seems that the error you are getting is related to the wavread function not being able to open the file. This could be due to a variety of reasons, such as incorrect file path or file format not being supported by the function.

One suggestion would be to check the file path and make sure it is pointing to the correct location of your .wav files. You can also try using the fullfile function to create the file path, as it ensures compatibility across different operating systems.

Another thing to check is the file format of your .wav files. Make sure they are in a format supported by the wavread function, such as 16-bit PCM or 24-bit PCM.

If the issue persists, you can try using a different function to load the .wav files, such as audioread or wavread2. These functions have similar syntax to wavread but may be more robust.

I hope this helps you resolve the issue and successfully load your .wav files in Matlab. If you continue to encounter problems, feel free to reach out for further assistance. Good luck with your project!
 

Related to Problem loading .wav files in matlab from directory

1. How do I load .wav files into Matlab from a directory?

To load .wav files into Matlab from a directory, you can use the built-in "audioread" function. This function takes in the file path of the directory and returns the audio data as a matrix, as well as the sampling frequency. You can also use the "dir" function to get a list of all the .wav files in the directory and then loop through each file to load them individually.

2. Why am I getting an error when trying to load .wav files in Matlab?

There could be several reasons for this error. One possibility is that the file path specified is incorrect, so make sure to double check the path. Another possibility is that the .wav file is corrupted or in a format that is not supported by Matlab. Lastly, make sure you are using the correct syntax for the "audioread" function as it requires the file path to be enclosed in single quotes.

3. How can I play the loaded .wav files in Matlab?

To play the loaded .wav files in Matlab, you can use the "sound" function. This function takes in the audio data and sampling frequency as inputs and plays the audio through your computer's speakers. You can also use the "audioplayer" function to create an audio player object and control the playback of the audio.

4. Can I load multiple .wav files at once in Matlab?

Yes, you can load multiple .wav files at once in Matlab by using the "dir" function to get a list of all the files in the directory and then using a for loop to load each file individually. You can also use the "audioread" function with the file path of each file as an input to load multiple files at once.

5. How can I manipulate the loaded .wav files in Matlab?

Once the .wav files are loaded into Matlab, you can manipulate them in various ways. For example, you can use the "plot" function to visualize the waveform of the audio, or use mathematical operations to modify the audio data. You can also apply filters, compressors, or other audio effects using the built-in functions in Matlab's Signal Processing Toolbox.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
15
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
12K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
841
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
361
  • Engineering and Comp Sci Homework Help
Replies
1
Views
929
  • Engineering and Comp Sci Homework Help
Replies
2
Views
868
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
164
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top