Thread Subject: Invalid File identifier on loop

Subject: Invalid File identifier on loop

From: Scott

Date: 22 Jun, 2009 21:04:01

Message: 1 of 6

I'm working with Matlab 2009a and have constructed the following simple routine:

fp = fopen('test.txt','w');
for i = 1:50
    fprintf(fp,'%4d hi, i am from hdsc\n',i);
end;
fclose(fp);

This works fine, and creates the file expected. However, when I do the following;
fp = fopen('test.txt','w');
for i = 1:500
    fprintf(fp,'%4d hi, i am from hdsc\n',i);
end;
fclose(fp);


I get the following;

??? Error using ==> fclose
Invalid file identifier. Use fopen to generate a valid file identifier.


What would be causing this

Subject: Invalid File identifier on loop

From: Matt Fig

Date: 22 Jun, 2009 21:16:02

Message: 2 of 6

Works fine here.

Subject: Invalid File identifier on loop

From: Jan Simon

Date: 22 Jun, 2009 23:09:01

Message: 3 of 6

Dear Scott!

> fp = fopen('test.txt','w');
> for i = 1:50
> fprintf(fp,'%4d hi, i am from hdsc\n',i);
> end;
> fclose(fp);
>
> This works fine, and creates the file expected. However, when I do the following;
> ...
> for i = 1:500
> ...
> ??? Error using ==> fclose
> Invalid file identifier. Use fopen to generate a valid file identifier.

The file identifier can get invalid during writing, if the disk is full or the quota is reached. Nevertheless, the error appears at closing and not during writing. This would mean, that the problem occurs, when the last line is written.
-- This is unlikely!
In addition, you would have more problem, if the disk is completely full.

Another source for this problem might be a TIMER function, which changes the variable "fp" after a certain time, which is by accident exactly the time after the last line has been written. Nevertheless, you would need a DRAWNOW anywhere in the code, to allow the TIMERFCN to operate.
-- Really unlikely!

So I assume, there is a completely other problem in some code, you do not show here.

Good luck, Jan

Subject: Invalid File identifier on loop

From: Scott

Date: 24 Jun, 2009 13:15:03

Message: 4 of 6

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <h1p2ud$2sp$1@fred.mathworks.com>...
> Dear Scott!
>
> > fp = fopen('test.txt','w');
> > for i = 1:50
> > fprintf(fp,'%4d hi, i am from hdsc\n',i);
> > end;
> > fclose(fp);
> >
> > This works fine, and creates the file expected. However, when I do the following;
> > ...
> > for i = 1:500
> > ...
> > ??? Error using ==> fclose
> > Invalid file identifier. Use fopen to generate a valid file identifier.
>
> The file identifier can get invalid during writing, if the disk is full or the quota is reached. Nevertheless, the error appears at closing and not during writing. This would mean, that the problem occurs, when the last line is written.
> -- This is unlikely!
> In addition, you would have more problem, if the disk is completely full.
>
> Another source for this problem might be a TIMER function, which changes the variable "fp" after a certain time, which is by accident exactly the time after the last line has been written. Nevertheless, you would need a DRAWNOW anywhere in the code, to allow the TIMERFCN to operate.
> -- Really unlikely!
>
> So I assume, there is a completely other problem in some code, you do not show here.
>
> Good luck, Jan

Actually, what I posted was the entirety of the code. The disk isn't full, and there aren't quotas. (It is writing to an NFS mount, but that should be no problem).

Subject: Invalid File identifier on loop

From: Jan Simon

Date: 24 Jun, 2009 21:56:01

Message: 5 of 6

Dear Scott!

> fp = fopen('test.txt','w');
> for i = 1:50
> fprintf(fp,'%4d hi, i am from hdsc\n',i);
> end;
> fclose(fp);
>
> This works fine, and creates the file expected. However, when I do the following;
> ...
> for i = 1:500
> ...
> ??? Error using ==> fclose
> Invalid file identifier. Use fopen to generate a valid file identifier.

Is it still not solved?! I'm really curious, because something magic must happen!
Did you try to find the minimal number of loops, which invalidates the file pointer? Please insert a "disp(fp)" before FCLOSE, so you could see, if the variable has been changed from anywhere.
It is remarkably, that fp does not loose the validity during writing, but exactly after the last line was written...

Curious, Jan

Subject: Invalid File identifier on loop

From: Sebastiaan

Date: 25 Aug, 2011 19:15:47

Message: 6 of 6

"Scott " <scott.mohnkern@noaa.gov> wrote in message <h1t8sn$ov9$1@fred.mathworks.com>...
> "Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <h1p2ud$2sp$1@fred.mathworks.com>...
> > Dear Scott!
> >
> > > fp = fopen('test.txt','w');
> > > for i = 1:50
> > > fprintf(fp,'%4d hi, i am from hdsc\n',i);
> > > end;
> > > fclose(fp);
> > >
> > > This works fine, and creates the file expected. However, when I do the following;
> > > ...
> > > for i = 1:500
> > > ...
> > > ??? Error using ==> fclose
> > > Invalid file identifier. Use fopen to generate a valid file identifier.
> >
> > The file identifier can get invalid during writing, if the disk is full or the quota is reached. Nevertheless, the error appears at closing and not during writing. This would mean, that the problem occurs, when the last line is written.
> > -- This is unlikely!
> > In addition, you would have more problem, if the disk is completely full.
> >
> > Another source for this problem might be a TIMER function, which changes the variable "fp" after a certain time, which is by accident exactly the time after the last line has been written. Nevertheless, you would need a DRAWNOW anywhere in the code, to allow the TIMERFCN to operate.
> > -- Really unlikely!
> >
> > So I assume, there is a completely other problem in some code, you do not show here.
> >
> > Good luck, Jan
>
> Actually, what I posted was the entirety of the code. The disk isn't full, and there aren't quotas. (It is writing to an NFS mount, but that should be no problem).

Not sure if replying on old posts is of any use: I also had this problem with NFS. On the client, there were no quota's. However, on the NFS server, there were, and that caused the problem.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
invalid file id... Scott 22 Jun, 2009 17:09:02
fclose Scott 22 Jun, 2009 17:09:02
fopen Scott 22 Jun, 2009 17:09:02
loop Scott 22 Jun, 2009 17:09:02
rssFeed for this Thread

Contact us at files@mathworks.com