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);

 採用された回答

Walter Roberson
Walter Roberson 2011 年 11 月 18 日

0 投票

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 件のコメント

huda nawaf
huda nawaf 2011 年 11 月 18 日
thanks Walter,
you are on right, the no. of files is just 17770.
many many thanks
huda nawaf
huda nawaf 2011 年 12 月 3 日
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
Walter Roberson
Walter Roberson 2011 年 12 月 3 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeSearch Path についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by