problem in read files
2 views (last 30 days)
Show older comments
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);
0 Comments
Accepted Answer
Walter Roberson
on 18 Nov 2011
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
Walter Roberson
on 3 Dec 2011
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.
More Answers (0)
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!