"Subscript indices must either be real positive integers or logicals"

1 view (last 30 days)
Hi,
I'm not sure if this is the right title, but I ran into a problem with my current code.
speed=[1511 1505.7 1490.2 1484.1 1480.3 1481.8 1485.4 1491 1506.3 1524 1548.6];
depth=[0 85 165 295 665 1005 1505 2005 3005 4005 5000];
phi=20;
i=1;
r=0;
while r<4000
n=1;
xn=1;
for i=1:11
c1=speed(1,i);
z1=depth(1,i);
z2=depth(1,i+1);
c2=speed(1,i+1);
a=sind(phi)/c1;
for z=z1:z2
m=(c2-c1)/(z2-z1);
n=c1-m*z1;
cn=m*z+n;
theta=asind(a*cn);
dx=(z-(z+1))*tand(theta);
xn=xn+dx
x(1,n)=abs(xn);
y(1,n)=z;
n=n+1
r=abs(xn);
end
end
phi=phi+5;
end
However, if I run it, I get the error
Subscript indices must either be real positive integers or logicals.
Error in ATOC (line 83)
x(1,n)=abs(xn);
which I don't understand, because n should be a positive integer...
Any ideas? Thank you very much!

Accepted Answer

Star Strider
Star Strider on 31 Oct 2015
You have two definitions of ‘n’ in your loop, first:
n=c1-m*z1;
then later:
n=n+1;
The first one is creating fractional values for ‘n’, and that is what is throwing the error. It occurs when the lengths of ‘x’ and ‘y’ each have 1511 elements. Since I do not know what you are doing, I cannot offer any specific solution.
  2 Comments
Star Strider
Star Strider on 1 Nov 2015
Anke Kügler’s ‘Answer’ is copied here:
Haha, that is indeed stupid and quite embarrassing. I totally forgot about the two n's. Thank you, that did solve my problem!
Star Strider
Star Strider on 1 Nov 2015
My pleasure!
The sincerest expression of appreciation here on MATLAB Answers is to Accept the Answer that most closely solves your problem.

Sign in to comment.

More Answers (1)

Anke Kügler
Anke Kügler on 31 Oct 2015
Haha, that is indeed stupid and quite embarrassing. I totally forgot about the two n's. Thank you, that did solve my problem!

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!