j=1;
[filename,filepath]=uigetfile('*.txt','Select Input file');
file = [filepath,filename];
fid = fopen(file,'rt');
if fid == -1
('Error opening the file')
end
while 1
nextline = fgetl(fid); %读第一行
DAY= nextline(1:2)
D(i)=str2num(DAY)
MONTH = nextline(4:6)
j=j+1;
if ~isstr(nextline), break, end %读到最后跳出
end
打开的文件内容为 20 Oct 2008
读数据正确 但总是出现错误提示:
??? Index exceeds matrix dimensions.
Error in ==> xishu at 10
DAY= nextline(1:2)
到底是为么事??
------解决方案--------------------------------------------------------
菜鸟帮菜鸟顶……
------解决方案--------------------------------------------------------
尾随朵朵而来~
DAY= nextline(1:2)
参数是否错误? nextline(1:2) -> nextline(1,2) (我瞎猜的哈~完全不晓得这是什么语言~)
------解决方案--------------------------------------------------------
[filename,filepath]=uigetfile('*.txt','Select Input file');
file = [filepath,filename];
fid = fopen(file,'rt');
if fid == -1
('Error opening the file')
end
txtline=fgetl(fid);
while ischar(txtline)
disp(txtline)
txtline=fgetl(fid);
end
fclose(fid);
要保持好的编程习惯每次打开文件不要忘了关闭