Path: news.mathworks.com!not-for-mail
From: Loren Shure <loren@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Wavread loop: Variable filename
Date: Fri, 19 Sep 2008 08:01:01 -0400
Organization: The MathWorks
Lines: 64
Message-ID: <MPG.233d5e8a1c997d179898d1@news.mathworks.com>
References: <gat6nh$c36$1@fred.mathworks.com> <gata66$d30$1@fred.mathworks.com> <gatahf$fsi$1@fred.mathworks.com>
NNTP-Posting-Host: shurel.dhcp.mathworks.com
Mime-Version: 1.0
Content-Type: text/plain; charset="iso-8859-15"
Content-Transfer-Encoding: 7bit
X-Trace: fred.mathworks.com 1221825661 7992 172.31.57.117 (19 Sep 2008 12:01:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 19 Sep 2008 12:01:01 +0000 (UTC)
User-Agent: MicroPlanet-Gravity/2.70.2067
Xref: news.mathworks.com comp.soft-sys.matlab:490948


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