From: Elliot on
Hi I am struggling to figure out the reason for this error message "index must be a positive integer or logical". Now the code works fine for positive and negative values that are input individual but when I use it in a for loop from -360 to 1 it can't handle negative numbers but works for 1 to 360 which is shown below. The code is quite long and I have put some of it in a function to simplify it a bit but I will display it all to see if anyone could see why it is not working.

Also is there a function or simple way to draw a line between two point in a 3-D plot?

clear all
ref=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25];

Mx=[0 9.5 7 7 12 12 13 6 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 14.5 4.5 6.5 12.5 6.5 12.5 9.5 9.5 9.5 9.5 9.5 9.5 7 12 12 13 6 9.5 7 9.5 9.5 14.5 4.5 14.5 4.5 7 7 12 12 9.5];% 18

My=[0 15 12.5 17.5 12.5 17.5 15 15 18.5 11.5 15 15 22.5 7.5 20.5 9.5 20.5 7.5 15 15 15 15 15 15 9.5 9.5 10 9 8 8 17.5 12.5 17.5 15 15 18.5 12.5 11.5 15 23 23 7 7 12.5 17.5 17.5 12.5 15]; %18

Mz=[0 14.5 14.5 14.5 14.5 14.5 14.5 14.5 14.5 14.5 29.5 23 23 23 27.5 27.5 18.5 20.5 23 23 26 26 18.5 18.5 20.5 19 18 17 16 15 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 13.5 13.5 13.5 13.5 13.5 13.5 13.5 13.5 9]; %18

zvecx = [0 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5];

zvecy = [0 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15];

zvecz = [0 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5];

Zvec = [zvecx;
zvecy;
zvecz];

TR = [Mx;My;Mz];
NZTR = TR-Zvec;
N=360;

for i=1:1:360
phi = i* pi/180; % The rotation angle, converted in radians
Zrot = [cos(phi) -sin(phi) 0; sin(phi) cos(phi) 0 ; 0 0 1]; % rotation about the z axis

q = Zrot*NZTR;
TB=q+Zvec;

xvecx = [0 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5 9.5];

xvecy = [0 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15];

xvecz = [0 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9];

Xvec = [xvecx;
xvecy;
xvecz];

NXTR = TB-Xvec;

gamma = 0 * pi/180; % The rotation angle, converted in radians
Xrot = [1 0 0; 0 cos(gamma) sin(gamma);0 -sin(gamma) cos(gamma)]; %rotation about the y-axis

q2=Xrot*NXTR;
NTB=q2+Xvec;

x2 = [NTB(1,:) 2 2 17 17 9.5 11.5 7.5];
y2 = [NTB(2,:) 2 26 2 26 10 19 19];
z2 = [NTB(3,:) 0 0 0 0 0 0 0];

xlabel('x')
ylabel('y')
zlabel('z')

lenx40=x2(40);
lenx41=x2(41);
lenx42=x2(42);
lenx43=x2(43);

leny40=y2(40);
leny41=y2(41);
leny42=y2(42);
leny43=y2(43);

lenz40=z2(40);
lenz41=z2(41);
lenz42=z2(42);
lenz43=z2(43);

D1(i)=sqrt((lenx40-11.5)^2+(leny40-19)^2+(lenz40-0)^2)
D2(i)=sqrt((lenx41-7.5)^2+(leny41-19)^2+(lenz41-0)^2)
D3(i)=sqrt((lenx42-9.5)^2+(leny42-10)^2+(lenz42-0)^2)
D4(i)=sqrt((lenx43-9.5)^2+(leny43-10)^2+(lenz43-0)^2)

hold on;

end
degpt=1:1:360;
title('Variation of the 4 distances with degrees in Z axis')
plot(degpt,D1,'black')
hold on
plot(degpt,D2)
hold on
plot(degpt,D3,'red');
hold on
plot(degpt,D4,'yellow');
From: Herve on
When you use the following for loop

for i=-360:1:1

you get the following error message :

??? Attempted to access D1(-360); index must be a positive integer or logical.

Error in ==> Untitled at 72
D1(i)=sqrt((lenx40-11.5)^2+(leny40-19)^2+(lenz40-0)^2)

The reason is that Matlab's arrays must have a positive index.
D1(-360) is not going to work...
You must have D1(i) with i a positive integer.