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:
Opening Multiple Files

Subject: Opening Multiple Files

From: Rachit

Date: 21 Jun, 2010 22:47:05

Message: 1 of 9

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

Subject: Opening Multiple Files

From: dpb

Date: 21 Jun, 2010 22:59:57

Message: 2 of 9

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);

--

Subject: Opening Multiple Files

From: Neil

Date: 22 Jun, 2010 00:31:22

Message: 3 of 9

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

Subject: Opening Multiple Files

From: dpb

Date: 22 Jun, 2010 01:48:19

Message: 4 of 9

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) :)

--

Subject: Opening Multiple Files

From: TideMan

Date: 22 Jun, 2010 04:59:41

Message: 5 of 9

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.

Subject: Opening Multiple Files

From: dpb

Date: 22 Jun, 2010 05:19:20

Message: 6 of 9

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...

--

Subject: Opening Multiple Files

From: Steven Lord

Date: 22 Jun, 2010 16:00:37

Message: 7 of 9


"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

Subject: Opening Multiple Files

From: dpb

Date: 22 Jun, 2010 16:23:04

Message: 8 of 9

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...

--

Subject: Opening Multiple Files

From: Jan Simon

Date: 23 Jun, 2010 17:15:22

Message: 9 of 9

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

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