用arcgis提取出的ascii文件,导入matlab中textread读取不了出现:
错误使用 dataread
从文件(第 1 行,第 1 个字段)中读取 number 时出现问
题 ==>
出错 textread (第 124 行)
[varargout{1:nlhs}]=dataread('file',varargin{:}); %#ok
以下是代码内容,希望能给出具体一点的修改方法,鄙人不胜感激!
format long;
[m,n]=size(FlowAcc);
UpY=DownY+cellsize*m;
Ac=floor(Ac/(cellsize^2)); %
len_ouletX=length(ouletX);
stepPoint=[]; WaterHead=[];
num_ToPoint=0; %
num_slope=0; %
%
for ilen=1:len_ouletX
%
startRow=floor((UpY-ouletY(ilen))/cellsize)+1;
startCol=floor((ouletX(ilen)-LeftX)/cellsize)+1;
stepPoint=[stepPoint;[startRow,startCol,FlowAcc(startRow,startCol),0,0]];
empty_stepPoint=1;
%
while empty_stepPoint
startRow=stepPoint(1,1);
startCol=stepPoint(1,2);
if FlowAcc(startRow,startCol)>=Ac
%
if startCol<n && FlowDir(startRow,startCol+1)==2^4 % && FlowAcc(startRow,startCol)>Ac % && FlowAcc(startRow,startCol+1)>Ac
num_ToPoint=num_ToPoint+1; %
%
if FlowAcc(startRow,startCol+1)>Ac; stepPoint=[stepPoint;[startRow,startCol+1,FlowAcc(startRow,startCol+1),startRow,startCol]]; end
%
if FlowAcc(startRow,startCol+1)<=Ac; num_slope=num_slope+1; end
end
if startRow<m && startCol<n && FlowDir(startRow+1,startCol+1)==2^5 % && FlowAcc(startRow,startCol)>Ac % && FlowAcc(startRow+1,startCol+1)>Ac
num_ToPoint=num_ToPoint+1; %
if FlowAcc(startRow+1,startCol+1)>Ac;stepPoint=[stepPoint;[startRow+1,startCol+1,FlowAcc(startRow+1,startCol+1),startRow,startCol]];end
if FlowAcc(startRow+1,startCol+1)<=Ac;num_slope=num_slope+1;end
end
if startRow<m && FlowDir(startRow+1,startCol)==2^6 % && FlowAcc(startRow,startCol)>Ac % && FlowAcc(startRow+1,startCol)>Ac
num_ToPoint=num_ToPoint+1; %
if FlowAcc(startRow+1,startCol)>Ac;stepPoint=[stepPoint;[startRow+1,startCol,FlowAcc(startRow+1,startCol),startRow,startCol]];end
if FlowAcc(startRow+1,startCol)<=Ac;num_slope=num_slope+1;end
end
if startRow<m && startCol>1 && FlowDir(startRow+1,startCol-1)==2^7 % && FlowAcc(startRow,startCol)>Ac % && FlowAcc(startRow+1,startCol-1)>Ac
num_ToPoint=num_ToPoint+1; %
if FlowAcc(startRow+1,startCol-1)>Ac;stepPoint=[stepPoint;[startRow+1,startCol-1,FlowAcc(startRow+1,startCol-1),startRow,startCol]];end
if FlowAcc(startRow+1,startCol-1)<=Ac;num_slope=num_slope+1;end
end
if startCol>1 && FlowDir(startRow,startCol-1)==2^0 % && FlowAcc(startRow,startCol)>Ac % && FlowAcc(startRow,startCol-1)>Ac
num_ToPoint=num_ToPoint+1; %
if FlowAcc(startRow,startCol-1)>Ac;stepPoint=[stepPoint;[startRow,startCol-1,FlowAcc(startRow,startCol-1),startRow,startCol]];end
if FlowAcc(startRow,startCol-1)<=Ac;num_slope=num_slope+1;end
end
if startRow>1 && startCol>1 && FlowDir(startRow-1,startCol-1)==2^1 % && FlowAcc(startRow,startCol)>Ac % && FlowAcc(startRow-1,startCol-1)>Ac
num_ToPoint=num_ToPoint+1; %
if FlowAcc(startRow-1,startCol-1)>Ac;stepPoint=[stepPoint;[startRow-1,startCol-1,FlowAcc(startRow-1,startCol-1),startRow,startCol]];end
if FlowAcc(startRow-1,startCol-1)<=Ac;num_slope=num_slope+1;end
end
if startRow>1 && FlowDir(startRow-1,startCol)==2^2 % && FlowAcc(startRow,startCol)>Ac % && FlowAcc(startRow-1,startCol)>Ac
num_ToPoint=num_ToPoint+1; %
if FlowAcc(startRow-1,startCol)>Ac;stepPoint=[stepPoint;[startRow-1,startCol,FlowAcc(startRow-1,startCol),startRow,startCol]];end
if FlowAcc(startRow-1,startCol)<=Ac;num_slope=num_slope+1;end
end
if startRow>1 && startCol<n && FlowDir(startRow-1,startCol+1)==2^3 % && FlowAcc(startRow,startCol)>Ac % && FlowAcc(startRow-1,startCol+1)>Ac
num_ToPoint=num_ToPoint+1; %
if FlowAcc(startRow-1,startCol+1)>Ac;stepPoint=[stepPoint;[startRow-1,startCol+1,FlowAcc(startRow-1,startCol+1),startRow,startCol]];end
if FlowAcc(startRow-1,startCol+1)<=Ac;num_slope=num_slope+1;end
end
%
if num_slope==num_ToPoint; WaterHead=[WaterHead;[startRow,startCol]]; end
end
num_slope=0; num_ToPoint=0;
stepPoint(1,:)=[]; % %
empty_stepPoint=~isempty(stepPoint);
end
end
WaterHead(:,1)=UpY-WaterHead(:,1).*cellsize+cellsize/2; % row,Y
WaterHead(:,2)=LeftX+WaterHead(:,2).*cellsize-cellsize/2; % Col,X
length_WaterHead=length(WaterHead);
str1='D:\program\head.txt';
str2='Head'; str3='.txt'; % str2=num2str(num1);
Pathi_Out=strcat(str1,str2,str3); % D:\RiverResult\MainDownS200\ProKs\ProKsi.txt
fid=fopen(Pathi_Out,'w');
for j=1:length_WaterHead
fprintf(fid,'%f\t',WaterHead(j,2)); % X-cordinate
fprintf(fid,'%f\n',WaterHead(j,1)); % Y-cordinate
end
fclose(fid);
应该就是在用 Matlab 的 textread 函数时,出问题了。第 124 行中,用了 dataread 函数,但文件中读取 number 时出现了问题。
由于没有只有部分代码,不好做判断。可以通过检查:
调用 textread 函数时传递给它的参数正确不
用其他 Matlab 函数(例如,importdata)读取 ASCII 文件。
该回答引用ChatGPT
请参考下面的解决方案,如果有帮助,还请点击 “采纳” 感谢支持!
这个错误信息表明,在读取文件的第一行第一个字段时出现了问题,导致 textread 函数出错。可能是文件格式不正确,或者第一行的内容不是数字。可以手动检查一下文件的格式是否正确,或者尝试使用其他函数读取文件。
、、、、、、、
需要详细分析,提供一下代码(文字),(勿提供截图)
格式不正确,需要你在运行前转换成正确的格式
可能是ASCII文件的格式不符合textread的读取要求,可以尝试更改读取格式或者使用其他读取数据的函数,例如fscanf、importdata等。如果您能提供更详细的文件信息,我们可以帮助更准确地定位问题并给出更有针对性的解决方案。
For ttoG
是要什么读取格式呢
看起来是读取格式有问题。可以尝试使用fopen和fscanf读取文件并指定读取格式,以确保数据正确读入。例如:
fid = fopen('filename.txt');
data = fscanf(fid, '%f %f %f', [3 Inf]);
fclose(fid);
请确保指定的读取格式与文件中数据的格式相匹配,否则可能会读取失败或读取错误的数据。
For ttoG
您之前说的更详细的文件信息是什么文件信息呢
我提到的"更详细的文件信息"是指你在使用arcgis提取的ASCII文件的格式、分隔符、行尾符、数据类型等关键信息。这些信息很重要,因为textread在读取文件时需要知道如何对文件中的数据进行解析。