1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Problem loading .wav files in matlab from directory

  1. Oct 3, 2014 #1
    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;
    D = dir(directory);
    for i=2 : size(D,1),
    [wav,fs,nbits] = wavread(D(i).name);
    wav = wav ( :,1 );
    data = add_wav(wav,data);
    wavs = zeros(size(data,2)-1,size(data,1));
    for i=2 : size(data,2),
    wavs( i-1,: ) = data ( :,i )';

    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
  2. jcsd
  3. Oct 3, 2014 #2


    Staff: Mentor

    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:


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

    Last edited by a moderator: Oct 3, 2014
  4. Oct 3, 2014 #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...
  5. Oct 3, 2014 #4


    Staff: Mentor

    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.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted

Similar Discussions: Problem loading .wav files in matlab from directory