problem in read files

hello,
I read files , and take the first column from each file and append it with the other column of other file.
I read 50000 files
this is my code:
%%%%%%%%%%%%%%
targetdir = 'd:\matlab7\work\training_set';
targetfiles = '*.txt';
fileinfo = dir(fullfile(targetdir, targetfiles))
k=0;arr(:,:)=0; inc=0;k=0;y=1;c0=[];
for i = 1:50000
thisfilename = fullfile(targetdir, fileinfo(i).name);
f=fopen(thisfilename,'r');
c = textscan(f,'%f %f %s','Delimiter',',','headerLines',1);
fclose(f);
c1=sparse(length(c));c2=sparse(length(c1));c3=sparse(length(c));
c1=c{1}; c01=[ c0;c1];
s=sort(c01);
id_u=unique(s); c0=id_u;
clear c1
end
%%%%%%%%%
why I got this error when i=17771.
??? Index exceeds matrix dimensions.
Error in ==> movfile at 7
thisfilename = fullfile(targetdir, fileinfo(i).name);

 Accepted Answer

Walter Roberson
Walter Roberson on 18 Nov 2011

0 votes

You should run your loop from 1 to length(fileinfo) . For whatever reason, dir() is only returning 17770 file names. Code that assumes a particular number of names in a directory is likely to crash eventually.
You should focus your efforts on figuring out why dir() is returning 17770 file names instead of the number you expect. For example the rest of the 50000 might not be *.txt files. Or perhaps there is a bug: I do not recall which MATLAB release you are using?

3 Comments

thanks Walter,
you are on right, the no. of files is just 17770.
many many thanks
hi,
I want read my file from 101
I wrote
fid = fopen('d:\matlab\r2011a\bin\flixsterdata\finalflix0.txt','r');
c = textscan(fid,'(%d,%d)\t%d','Delimiter',',','headerLines', 100);
c1=c{1};
LL=length(c1)
why length of c1 is 1, the length of original file finalflix0 is more than 1000 values
the structure of my file:
(1,1) 100
(2,1) 4
...etc
thanks in advance
The length would be 1 if your format does not match the content of the file. For example if the actual file uses space instead of tab.

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!