Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Wavread loop: Variable filename

Subject: Wavread loop: Variable filename

From: Koos Spee

Date: 18 Sep, 2008 09:24:01

Message: 1 of 12

Hello,

I have a set of (wav)files e.g.

file01.wav, file02.wav, file03.wav etc.

Now, I want to get load these files into Matlab with a loop and storing them into load01, load02, load03 etc

However I am not able to give them the correct filename. I tried for example:

for i=1:3
    StrI=int2str(i);
    WavName='file';
    WavStored='load';
    temp=strcat(WavName, StrI);
    strcat(WavStored, StrI)=wavread(temp);
end

Also looked into dir('*wav'), but that leads to the same problem
 

Subject: Wavread loop: Variable filename

From: Dave Brackett

Date: 18 Sep, 2008 09:31:01

Message: 2 of 12

"Koos Spee" <bmx_360@hotmail.com> wrote in message <gat6nh$c36$1@fred.mathworks.com>...
> Hello,
>
> I have a set of (wav)files e.g.
>
> file01.wav, file02.wav, file03.wav etc.
>
> Now, I want to get load these files into Matlab with a loop and storing them into load01, load02, load03 etc
>
> However I am not able to give them the correct filename. I tried for example:
>
> for i=1:3
> StrI=int2str(i);
> WavName='file';
> WavStored='load';
> temp=strcat(WavName, StrI);
> strcat(WavStored, StrI)=wavread(temp);
> end
>
> Also looked into dir('*wav'), but that leads to the same problem
>

you are missing the 0 out of the file name. include it in the string like this:
temp=strcat(WavName,'0',StrI)

Subject: Wavread loop: Variable filename

From: Koos Spee

Date: 18 Sep, 2008 09:49:01

Message: 3 of 12

"Dave Brackett" <davebrackett@hotmail.com> wrote in message <gat74l$fol$1@fred.mathworks.com>...
>
> you are missing the 0 out of the file name. include it in the string like this:
> temp=strcat(WavName,'0',StrI)


True, but that is just an error in the example I above.

The point is "strcat(WaveName, StrI)=wavread()" does not work.

Subject: Wavread loop: Variable filename

From: Dave Brackett

Date: 18 Sep, 2008 10:05:04

Message: 4 of 12

"Koos Spee" <bmx_360@hotmail.com> wrote in message <gat86d$oo6$1@fred.mathworks.com>...
> "Dave Brackett" <davebrackett@hotmail.com> wrote in message <gat74l$fol$1@fred.mathworks.com>...
> >
> > you are missing the 0 out of the file name. include it in the string like this:
> > temp=strcat(WavName,'0',StrI)
>
>
> True, but that is just an error in the example I above.
>
> The point is "strcat(WaveName, StrI)=wavread()" does not work.

ok, splitting that line into 2 works, i.e.

    WavStored_str=strcat(WavStored,'0',StrI);
    WavStored_str=wavread(temp);

Subject: Wavread loop: Variable filename

From: Koos Spee

Date: 18 Sep, 2008 10:22:02

Message: 5 of 12


>
> ok, splitting that line into 2 works, i.e.
>
> WavStored_str=strcat(WavStored,'0',StrI);
> WavStored_str=wavread(temp);
>
>


That only gives me the last file of the loop (named: WavStored_str) to work with.

Subject: Wavread loop: Variable filename

From: Steve Amphlett

Date: 18 Sep, 2008 10:23:02

Message: 6 of 12

"Koos Spee" <bmx_360@hotmail.com> wrote in message <gat6nh$c36$1@fred.mathworks.com>...
> Hello,
>
> I have a set of (wav)files e.g.
>
> file01.wav, file02.wav, file03.wav etc.
>
> Now, I want to get load these files into Matlab with a loop and storing them into load01, load02, load03 etc
>
> However I am not able to give them the correct filename. I tried for example:
>
> for i=1:3
> StrI=int2str(i);
> WavName='file';
> WavStored='load';
> temp=strcat(WavName, StrI);
> strcat(WavStored, StrI)=wavread(temp);
> end
>
> Also looked into dir('*wav'), but that leads to the same problem

What about something different like this (untested):

files=dir('*.wav');
for file = files
  file.data=wavread(file.name);
end

Subject: Wavread loop: Variable filename

From: Steve Amphlett

Date: 18 Sep, 2008 10:29:03

Message: 7 of 12

"Steve Amphlett" <Firstname.Lastname@Where-I-Work.com> wrote in message <gata66$d30$1@fred.mathworks.com>...
> "Koos Spee" <bmx_360@hotmail.com> wrote in message <gat6nh$c36$1@fred.mathworks.com>...
> > Hello,
> >
> > I have a set of (wav)files e.g.
> >
> > file01.wav, file02.wav, file03.wav etc.
> >
> > Now, I want to get load these files into Matlab with a loop and storing them into load01, load02, load03 etc
> >
> > However I am not able to give them the correct filename. I tried for example:
> >
> > for i=1:3
> > StrI=int2str(i);
> > WavName='file';
> > WavStored='load';
> > temp=strcat(WavName, StrI);
> > strcat(WavStored, StrI)=wavread(temp);
> > end
> >
> > Also looked into dir('*wav'), but that leads to the same problem
>
> What about something different like this (untested):
>
> files=dir('*.wav');
> for file = files
> file.data=wavread(file.name);
> end

Should have tested!

files=dir('*.wav');
  for n=1:length(files);
  files(n).data=wavread(files(n).name);
end

Subject: Wavread loop: Variable filename

From: Koos Spee

Date: 18 Sep, 2008 11:24:02

Message: 8 of 12

Great!!

>
> Should have tested!
>
> files=dir('*.wav');
> for n=1:length(files);
> files(n).data=wavread(files(n).name);
> end
>

Subject: Wavread loop: Variable filename

From: Steven Lord

Date: 19 Sep, 2008 05:16:57

Message: 9 of 12


"Koos Spee" <bmx_360@hotmail.com> wrote in message
news:gat86d$oo6$1@fred.mathworks.com...
> "Dave Brackett" <davebrackett@hotmail.com> wrote in message
> <gat74l$fol$1@fred.mathworks.com>...
>>
>> you are missing the 0 out of the file name. include it in the string like
>> this:
>> temp=strcat(WavName,'0',StrI)
>
>
> True, but that is just an error in the example I above.
>
> The point is "strcat(WaveName, StrI)=wavread()" does not work.

Correct. You shouldn't create variables with names like this. See Q4.6 in
the newsgroup FAQ:

http://matlabwiki.mathworks.com/MATLAB_FAQ

Use a cell array or a struct array.

--
Steve Lord
slord@mathworks.com

Subject: Wavread loop: Variable filename

From: Loren Shure

Date: 19 Sep, 2008 12:01:01

Message: 10 of 12

In article <gatahf$fsi$1@fred.mathworks.com>, Firstname.Lastname@Where-
I-Work.com says...
> "Steve Amphlett" <Firstname.Lastname@Where-I-Work.com> wrote in message <gata66$d30$1@fred.mathworks.com>...
> > "Koos Spee" <bmx_360@hotmail.com> wrote in message <gat6nh$c36$1@fred.mathworks.com>...
> > > Hello,
> > >
> > > I have a set of (wav)files e.g.
> > >
> > > file01.wav, file02.wav, file03.wav etc.
> > >
> > > Now, I want to get load these files into Matlab with a loop and storing them into load01, load02, load03 etc
> > >
> > > However I am not able to give them the correct filename. I tried for example:
> > >
> > > for i=1:3
> > > StrI=int2str(i);
> > > WavName='file';
> > > WavStored='load';
> > > temp=strcat(WavName, StrI);
> > > strcat(WavStored, StrI)=wavread(temp);
> > > end
> > >
> > > Also looked into dir('*wav'), but that leads to the same problem
> >
> > What about something different like this (untested):
> >
> > files=dir('*.wav');
> > for file = files
> > file.data=wavread(file.name);
> > end
>
> Should have tested!
>
> files=dir('*.wav');
> for n=1:length(files);
> files(n).data=wavread(files(n).name);
> end
>
>
>

This is likely to cause issues as files gets redefined in a loop with a
control variable named files. Please follow the suggestions from the
FAQ instead! Or how about this:


files=dir('*.wav');
for n=1:length(files);
    basename = files(n).name;
    basename(end-3:end) = '';
    data.(basename)=wavread(files(n).name);
end


>> data
data =
      gong: [42028x1 double]
    handel: [73113x1 double]



--
Loren
http://blogs.mathworks.com/loren

Subject: Wavread loop: Variable filename

From: Steve Amphlett

Date: 19 Sep, 2008 12:56:02

Message: 11 of 12

Loren Shure <loren@mathworks.com> wrote in message > >
> > files=dir('*.wav');
> > for n=1:length(files);
> > files(n).data=wavread(files(n).name);
> > end
> >
> >
> >
>
> This is likely to cause issues as files gets redefined in a loop with a
> control variable named files.

I understood that 1:length(files) would be expanded once and then n assigned to each column of the expansion in turn. There are many posts here asking why modification of the "loop variable" inside the loop has no effect.

- Steve

Subject: Wavread loop: Variable filename

From: David Starkweather

Date: 6 Dec, 2008 21:48:02

Message: 12 of 12

Loren Shure <loren@mathworks.com> wrote in message <MPG.233d5e8a1c997d179898d1@news.mathworks.com>...
> In article <gatahf$fsi$1@fred.mathworks.com>, Firstname.Lastname@Where-
> I-Work.com says...
> > "Steve Amphlett" <Firstname.Lastname@Where-I-Work.com> wrote in message <gata66$d30$1@fred.mathworks.com>...
> > > "Koos Spee" <bmx_360@hotmail.com> wrote in message <gat6nh$c36$1@fred.mathworks.com>...
> > > > Hello,
> > > >
> > > > I have a set of (wav)files e.g.
> > > >
> > > > file01.wav, file02.wav, file03.wav etc.
> > > >
> > > > Now, I want to get load these files into Matlab with a loop and storing them into load01, load02, load03 etc
> > > >
> > > > However I am not able to give them the correct filename. I tried for example:
> > > >
> > > > for i=1:3
> > > > StrI=int2str(i);
> > > > WavName='file';
> > > > WavStored='load';
> > > > temp=strcat(WavName, StrI);
> > > > strcat(WavStored, StrI)=wavread(temp);
> > > > end
> > > >
> > > > Also looked into dir('*wav'), but that leads to the same problem
> > >
> > > What about something different like this (untested):
> > >
> > > files=dir('*.wav');
> > > for file = files
> > > file.data=wavread(file.name);
> > > end
> >
> > Should have tested!
> >
> > files=dir('*.wav');
> > for n=1:length(files);
> > files(n).data=wavread(files(n).name);
> > end
> >
> >
> >
>
> This is likely to cause issues as files gets redefined in a loop with a
> control variable named files. Please follow the suggestions from the
> FAQ instead! Or how about this:
>
>
> files=dir('*.wav');
> for n=1:length(files);
> basename = files(n).name;
> basename(end-3:end) = '';
> data.(basename)=wavread(files(n).name);
> end
>
>
> >> data
> data =
> gong: [42028x1 double]
> handel: [73113x1 double]
>
>
>
> --
> Loren
> http://blogs.mathworks.com/loren

Even with these techniques, I get "out of memory error". I am trying to read in all wav files in a given directory. So far, it will only work for 2 or 3 files and then the error message. Does anyone know if this has been a problem with earlier versions of matlab ? (I am using Matlab 6.5 R13) Even 'clear' and 'pack after each loop iteration doesnt help.

dgs.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us