why must rerun this code to pursue running

1 vue (au cours des 30 derniers jours)
huda nawaf
huda nawaf le 25 Jan 2012
hi, below my code, it is read stream of files from folder and update these files and write it again to another folder(contain old files and write over it).
i have 17770 files, the problem is :
the code read roughly 250 files, then give me error message:
??? Error using ==> fprintf
Invalid file identifier. Use fopen to generate a valid file
identifier.
so, i must close the matlab then open it and read again from the the stopped poin
this is my code:
format long
targetdir1 = 'd:\matlab\r2011a\bin\newtraining_set';
targetfiles1 = '*.txt';
fileinfo1 = dir(fullfile(targetdir1, targetfiles1));
targetdir = 'd:\matlab\r2011a\bin\training_set';
targetfiles = '*.txt';
fileinfo = dir(fullfile(targetdir, targetfiles));%%%%%%%%no. of files 17770
% for i =1:250%(fileinfo)
k=1;
thisfilename = fullfile(targetdir, fileinfo(i).name);
f = fopen(thisfilename,'r');
c = textscan(f, '%f %f %s', 'Delimiter', ',', 'headerLines', 1);
fclose(f);
c1 = c{1};c2=c{2}; c3=c{3};
dat=round(datenum(c3,'yyyy-mm-dd'));
for k1=1:length(c1)
file(k,1)=c1(k1);file(k,2)=c2(k1);file(k,3)=dat(k1);
k=k+1;
end
[x x1]=size(file);
thisfilename1 = fullfile(targetdir1, fileinfo1(i).name);
f1 = fopen(thisfilename1,'w');
for i=1:length(c1)
for j=1:3
fprintf(f1,'%d ',file(i,j));
end
fprintf(f1,'\n'); end
file(1:x,1:x1)=0; end; fclose(f1);

Réponses (2)

per isakson
per isakson le 25 Jan 2012
Make the following change to your code:
f = fopen(thisfilename,'r');
to
f = fopen(thisfilename,'r');
assert( f >=3, ... )
  2 commentaires
Walter Roberson
Walter Roberson le 25 Jan 2012
fid will be 0 on success, -1 on error, so asserting that it is >= 3 is not going to be useful.
per isakson
per isakson le 25 Jan 2012
@Walter
My mistake! I intended to propose a check on the file identifier. Now I have corrected my answere.

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 26 Jan 2012
It is known that some versions of MATLAB "leak" file identifiers for some commands (i.e., by not closing files used internally). There is also evidence that some versions of MATLAB "use up" internal file slots (which is different than file identifiers) when they should not, not "recycling" the slots.
In the case of "leaked" file identifiers, "close all" will recover them.
In the case of using up file slots, the problem is either internal to MATLAB or internal to the operating system, and is not something you can solve yourself.
I have never seen either kind of leak pinned down well. I have seen people have problems over multiple versions, and in both MS Windows and Linux (under different circumstances.) The running-out-after-250 is more associated with MS Windows.

Catégories

En savoir plus sur Testing Frameworks dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by