Opening Multiple Files

  • Follow


I am using the function to open a file 

fid=fopen('C:\Matlab\Specimen1.dat');

My file keeps on changing from Specimen 1-7. I Wanted to use the counter to open the files. I tried using :  
i=1;
While i~=8
fid=fopen('C:\Matlab\Specimen' {i} '.dat');
i= i +1;

but it was giving me an error. Can anyone suggest me how to proceed?

Thanks,

Rac
0
Reply Rachit 6/21/2010 10:47:05 PM

Rachit wrote:
> I am using the function to open a file
> fid=fopen('C:\Matlab\Specimen1.dat');
> 
> My file keeps on changing from Specimen 1-7. I Wanted to use the counter 
> to open the files. I tried using :  i=1;
> While i~=8
fn = ['C:\Matlab\Specimen' num2str{i} '.dat']
fid=fopen(fn);

--
0
Reply dpb 6/21/2010 10:59:57 PM


dpb <none@non.net> wrote in message <hvor2a$99e$1@news.eternal-september.org>...
> Rachit wrote:
> > I am using the function to open a file
> > fid=fopen('C:\Matlab\Specimen1.dat');
> > 
> > My file keeps on changing from Specimen 1-7. I Wanted to use the counter 
> > to open the files. I tried using :  i=1;
> > While i~=8
> fn = ['C:\Matlab\Specimen' num2str{i} '.dat']
> fid=fopen(fn);
> 
> --
Syntax isn't 100% correct here. 
Should read
fn = ['C:\Matlab\Specimen' num2str(i) '.dat']
fid=fopen(fn);
Presonally I prefer using sprintf , and for loops too
for i = 1:8
    fid = fopen(sprintf('C:\Matlab\Specimen%d.dat',i));
end

Remeber to close all open files too. If you don't you can exhaust the number of files that windows will allow you to open. 
for i = 1:8
    fid = fopen(sprintf('C:\Matlab\Specimen%d.dat',i));
    %Do what you need to do on each file
    fclose(fid);
end
Or store the fid's in an array and close them all when you are done
0
Reply Neil 6/22/2010 12:31:22 AM

Neil wrote:
> dpb <none@non.net> wrote in message 
> <hvor2a$99e$1@news.eternal-september.org>...
>> Rachit wrote:
>> > I am using the function to open a file
>> > fid=fopen('C:\Matlab\Specimen1.dat');
>> > > My file keeps on changing from Specimen 1-7. I Wanted to use the 
>> counter > to open the files. I tried using :  i=1;
>> > While i~=8
>> fn = ['C:\Matlab\Specimen' num2str{i} '.dat']
>> fid=fopen(fn);
>>
>> -- 
> Syntax isn't 100% correct here. Should read
> fn = ['C:\Matlab\Specimen' num2str(i) '.dat']
....

Ooh...thanks for catching the typo.  Altho took me blowing up the font 
size in the newsreader to see what the problem/difference was... :)

> Remeber to close all open files too. ...

And of course, while we're remembering things for OP, don't use "i" for 
loop variables in Matlab; it's defined internally as sqrt(-1)  :)

--
0
Reply dpb 6/22/2010 1:48:19 AM

On Jun 22, 12:31=A0pm, "Neil " <neil.i...@gmail.com> wrote:
> dpb <n...@non.net> wrote in message <hvor2a$99...@news.eternal-september.=
org>...
> > Rachit wrote:
> > > I am using the function to open a file
> > > fid=3Dfopen('C:\Matlab\Specimen1.dat');
>
> > > My file keeps on changing from Specimen 1-7. I Wanted to use the coun=
ter
> > > to open the files. I tried using : =A0i=3D1;
> > > While i~=3D8
> > fn =3D ['C:\Matlab\Specimen' num2str{i} '.dat']
> > fid=3Dfopen(fn);
>
> > --
>
> Syntax isn't 100% correct here.
> Should read
> fn =3D ['C:\Matlab\Specimen' num2str(i) '.dat']
> fid=3Dfopen(fn);
> Presonally I prefer using sprintf , and for loops too
> for i =3D 1:8
> =A0 =A0 fid =3D fopen(sprintf('C:\Matlab\Specimen%d.dat',i));
> end
>
> Remeber to close all open files too. If you don't you can exhaust the num=
ber of files that windows will allow you to open.
> for i =3D 1:8
> =A0 =A0 fid =3D fopen(sprintf('C:\Matlab\Specimen%d.dat',i));
> =A0 =A0 %Do what you need to do on each file
> =A0 =A0 fclose(fid);
> end
> Or store the fid's in an array and close them all when you are done

I'm interested to know why people use sprintf in preference to
num2str.
To me:

datfile=3D['C:\Matlab\Specimen' num2str(indx) '.dat'];
fid=3Dfopen(datfile,'rt');

is cleaner and easier to read/debug because everything defining the
file name is consecutive.

Whereas:

fid =3D fopen(sprintf('C:\Matlab\Specimen%d.dat',indx),'rt');

is a bit contorted; it doesn't read easily because indx comes
after .dat, whereas in the filename it actually comes before .dat, as
it does using num2str.


0
Reply mulgor (2848) 6/22/2010 4:59:41 AM

TideMan wrote:
> On Jun 22, 12:31 pm, "Neil " <neil.i...@gmail.com> wrote:
>> dpb <n...@non.net> wrote in message <hvor2a$99...@news.eternal-september.org>...
>>> Rachit wrote:
>>>> I am using the function to open a file
>>>> fid=fopen('C:\Matlab\Specimen1.dat');
>>>> My file keeps on changing from Specimen 1-7. I Wanted to use the counter
>>>> to open the files. I tried using :  i=1;
>>>> While i~=8
>>> fn = ['C:\Matlab\Specimen' num2str{i} '.dat']
>>> fid=fopen(fn);
>>> --
>> Syntax isn't 100% correct here.
>> Should read
>> fn = ['C:\Matlab\Specimen' num2str(i) '.dat']
>> fid=fopen(fn);
>> Presonally I prefer using sprintf , and for loops too
>> for i = 1:8
>>     fid = fopen(sprintf('C:\Matlab\Specimen%d.dat',i));
>> end
>>
>> Remeber to close all open files too. If you don't you can exhaust the number of files that windows will allow you to open.
>> for i = 1:8
>>     fid = fopen(sprintf('C:\Matlab\Specimen%d.dat',i));
>>     %Do what you need to do on each file
>>     fclose(fid);
>> end
>> Or store the fid's in an array and close them all when you are done
> 
> I'm interested to know why people use sprintf in preference to
> num2str.
> To me:
> 
> datfile=['C:\Matlab\Specimen' num2str(indx) '.dat'];
> fid=fopen(datfile,'rt');
> 
> is cleaner and easier to read/debug because everything defining the
> file name is consecutive.
> 
> Whereas:
> 
> fid = fopen(sprintf('C:\Matlab\Specimen%d.dat',indx),'rt');
> 
> is a bit contorted; it doesn't read easily because indx comes
> after .dat, whereas in the filename it actually comes before .dat, as
> it does using num2str.

In an instance such as this I don't see anything to necessarily commend 
it, either--num2str() converts internal to asii/character representation 
  w/o blanks which is what's wanted; sprintf() does the same w/ the 
necessary format string.  Guess it's an appearance to the beholder thing...

--
0
Reply none1568 (6654) 6/22/2010 5:19:20 AM

"TideMan" <mulgor@gmail.com> wrote in message 
news:0f76dfec-fc1c-4432-a65d-67108010a04d@v29g2000prb.googlegroups.com...
On Jun 22, 12:31 pm, "Neil " <neil.i...@gmail.com> wrote:
> dpb <n...@non.net> wrote in message 
> <hvor2a$99...@news.eternal-september.org>...

*snip*

> I'm interested to know why people use sprintf in preference to
> num2str.
> To me:
>
> datfile=['C:\Matlab\Specimen' num2str(indx) '.dat'];
> fid=fopen(datfile,'rt');
>
> is cleaner and easier to read/debug because everything defining the
> file name is consecutive.
>
> Whereas:
>
> fid = fopen(sprintf('C:\Matlab\Specimen%d.dat',indx),'rt');
>
> is a bit contorted; it doesn't read easily because indx comes
> after .dat, whereas in the filename it actually comes before .dat, as
> it does using num2str.

I think it's mostly personal preference.

For me, in the case where only one element changes?  NUM2STR is fine.  For 
the case where multiple elements change, the NUM2STR case gets a bit "busy" 
for my tastes.

datfile=['C:\Matlab\Specimen' num2str(indx) '_Task' num2str(index2) '.dat'];
fid=fopen(datfile,'rt');

versus

datfile = sprintf('C:\Matlab\Specimen%d_Task%d.dat', indx, index2);
fid = fopen(datfile,'rt');

In addition, if one input needs to be reused, SPRINTF has (and has had for 
several releases now) a little trick you can do to avoid having to specify 
that input N times.

x = 1;
s = sprintf('a%1$db%1$dc%1$dd%1$de', x) % a1b1c1d1e

% or even more confusingly

x = 1;
y = 2;
s = sprintf('a%2$db%1$dc', x, y) % prints a2b1c

This is documented in the "Identifier" section of the documentation for the 
"format" input argument.

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sprintf.html


But of course, there are other approaches given in Q4.12 in the newsgroup 
FAQ for this specific problem.  Why SPRINTF or NUM2STR?  Just DIR:

thepath = 'c:\Matlab';
D = dir(fullfile(thepath, 'Specimen*.dat'));
for k =1:numel(D)
    fid = fopen(fullfile(thepath, D(k).name), 'rt');
    % do stuff
    fclose(fid);
end


-- 
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on 
http://www.mathworks.com 


0
Reply slord (13285) 6/22/2010 4:00:37 PM

Steven Lord wrote:
> "TideMan" <mulgor@gmail.com> wrote in message 
> news:0f76dfec-fc1c-4432-a65d-67108010a04d@v29g2000prb.googlegroups.com...
....
>> I'm interested to know why people use sprintf in preference to
>> num2str.
....
> I think it's mostly personal preference.
> 
> For me, in the case where only one element changes?  NUM2STR is fine.  For 
> the case where multiple elements change, the NUM2STR case gets a bit "busy" 
> for my tastes.
....

> In addition, if one input needs to be reused, SPRINTF has (and has had for 
> several releases now) a little trick you can do to avoid having to specify 
> that input N times.
> 
> x = 1;
> s = sprintf('a%1$db%1$dc%1$dd%1$de', x) % a1b1c1d1e
> 
> % or even more confusingly
> 
> x = 1;
> y = 2;
> s = sprintf('a%2$db%1$dc', x, y) % prints a2b1c
....

Ouch...that hurts my eyes... :)  !!!

Useful, no doubt, altho I can't recall an instance of needing that.

I'd much rather have seen a repeat count on fields to eliminate (or at 
least minimize) the repmat() foolishness one has to resort to now.

> But of course, there are other approaches given in Q4.12 in the newsgroup 
> FAQ for this specific problem.  Why SPRINTF or NUM2STR?  Just DIR:
....

Very good point for opening existing files, indeed...

--
0
Reply dpb 6/22/2010 4:23:04 PM

Dear TideMan,

> I'm interested to know why people use sprintf in preference to
> num2str.

Efficiency.
Matlab 2009a, 1.5GHz Pentium-M, WinXP:
  tic; for k = 1:2000; v = num2str(k); clear('v'); end; toc
>> 0.18 sec
  tic; for k = 1:2000; v = sprintf('%d', k); clear('v'); end; toc
>> 0.07 sec
The CLEAR reduces the influence of the JIT.

Obviously you cannot feel the speed difference in the example of the OP.
The advantage of SPRINTF is bigger for floating point values:
  tic; for k = 1:2000; v = num2str(sin(k)); clear('v'); end; toc
>> 0.43 sec
  tic; for k = 1:2000; v = sprintf('%d', sin(k)); clear('v'); end; toc
>> 0.07 sec

Jan
0
Reply Jan 6/23/2010 5:15:22 PM

8 Replies
344 Views

(page loaded in 0.127 seconds)

Similiar Articles:













7/23/2012 10:46:15 AM


Reply: