Index in position 1 exceeds array bounds.

2 visualizzazioni (ultimi 30 giorni)
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 Commenti
Jeff Beck
Jeff Beck il 13 Gen 2023
Modificato: Jeff Beck il 13 Gen 2023
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 il 14 Gen 2023
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

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 13 Gen 2023
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 Commento
Walter Roberson
Walter Roberson il 14 Gen 2023
I suggest you consider using readmatrix -- it will automatically ignore header lines.

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by