why must rerun this code to pursue running
2 次查看(过去 30 天)
显示 更早的评论
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);
0 个评论
回答(2 个)
per isakson
2012-1-25
Make the following change to your code:
f = fopen(thisfilename,'r');
to
f = fopen(thisfilename,'r');
assert( f >=3, ... )
2 个评论
Walter Roberson
2012-1-25
fid will be 0 on success, -1 on error, so asserting that it is >= 3 is not going to be useful.
per isakson
2012-1-25
@Walter
My mistake! I intended to propose a check on the file identifier. Now I have corrected my answere.
Walter Roberson
2012-1-26
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Testing Frameworks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!