As the Headline says, i have a problem with the error: Index in Position 1 exceeds array bounds, in Line 4.
I need the code for a Porject at University, but cant help myself.
fid = fopen('Profile_AlpineValleys.txt');
Z = fscanf(fid,'%g',[2,inf]);
fclose(fid);
a=Z(1,:);
b=Z(2,:);
xsize = a(length(a));
nx = length(a);
dx = xsize/(nx-1);
x = -xsize/2:dx:xsize/2;
year = 365.25*24*60*60;
ka = 500 * year;
ma = 1E6*year;
tend = 10*ma;
dt = 50 * ka
dt = 7.8894e+11
ntimeSteps = 1;
time=0;
kappa = 10 / ka
kappa = 6.3376e-10
figure(1);
for i=1:1:nx
E0_imp(i)= b(i);
end
s=kappa * dt/(dx*dx);
A=sparse(nx,nx);
for i=2:nx-1
A(i,i-1)=-s;
A(i,i)=(1+2*s);
A(i,i+1)=-s;
end
A(1,1)=1;
A(nx,nx)=1;
while time < (tend + dt);
rhs = zeros(nx,1);
rhs(2:nx-1) = E0_imp(2:nx-1);
rhs(nx)=b(length(b));
E1_imp=A\rhs;
Tmean=sum(E1_imp())/nx;
clf;
subplot(1,1,1);
hold all
grid on
plot(a/1000,b/1000,'r' , 'LineWidth',1);
plot(a/1000,E1_imp/1000,'b' , 'LineWidth',1);
axis([0 50 0.5 2.5]);
title(['Erosionsrate: Zeit=',num2str(time/ma,'%.2f'),'MaDurchschnittshöhe=',num2str(Tmean,'%.2f'),' m']);
xlabel('x,km');
ylabel('Höhe, km');
drawnow
time=time+dt;
ntimeSteps=ntimeSteps + 1;
E0_imp=E1_imp;
end

댓글 수: 2

Jeff Beck
Jeff Beck 2023년 1월 13일
편집: Jeff Beck 2023년 1월 13일
I typed in a file test.txt with the following content:
1.048 2.048
9.048 10.048
12.048 13.048
I then ran the following lines from your code:
fid = fopen('test.txt');
Z = fscanf(fid,'%g',[2,inf]);
fclose(fid);
a = Z(1,:)
which gave the following output:
a =
1.0480 9.0480 12.0480
It would be helpful to see a portion of your Profile_AlpineValleys.txt file to understand what the issue is. It would also be helpful to know what the value of Z is. For mine:
>> Z
Z =
1.0480 9.0480 12.0480
2.0480 10.0480 13.0480
The error message suggests that your Z is empty indicating that fscanf was unable to parse the file. You might also check the value of fid. If it's -1, then you might need to confirm that the file is in the directory you are running in.
Leo
Leo 2023년 1월 14일
Thank you!
Helped me a lot. I loaded the file in MatLab and added it to the current path, so it worked out.
Have a nice Weekend!
Much of Love

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2023년 1월 13일

0 개 추천

Your fscanf was not able to read any data. Either the file is empty or else the first non-whitespace in it is not in the form of a floating point number.
For example your code does not handle the possibility of a text header line.

댓글 수: 1

I suggest you consider using readmatrix -- it will automatically ignore header lines.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

릴리스

R2022b

태그

질문:

Leo
2023년 1월 13일

댓글:

2023년 1월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by