# Index exceeds the number of array elements (0).

3 views (last 30 days)
Ryan DiTullio on 7 Mar 2020
Answered: Geoff Hayes on 7 Mar 2020
Hey everyone. I need help with my code. The code worked fine when I only had 3 joints, but I needed to add a 4th joint and when I did that I started to get the error for T4 in line 40. Would someone please help! Thank you in advance:
clear;
clc;
disp('Joint 1');
disp('==================================');
angle1 = input('Please keyin theta_1 (degree): ');
d1 = input('Please keyin distance 1 (d): ');
al1 = input('Please keyin alpha_1 (degree): ');
disp('Joint 2');
disp('==================================');
angle2 = input('Please keyin theta_2 (degree): ');
d2 = input('Please keyin distance 2 (d): ');
al2 = input('Please keyin alpha_2 (degree): ');
disp('Joint 3');
disp('==================================');
angle3 = input('Please keyin theta_3 (degree): ');
d3 = input('Please keyin distance 3 (d): ');
al3 = input('Please keyin alpha_3 (degree): ');
disp('Joint 4');
disp('==================================');
angle4 = input('Please keyin theta_4 (degree): ');
d4 = input('Please keyin distance 4 (d): ');
al4 = input('Please keyin alpha_4 (degree): ');
theta1 = 0:angle1/100:angle1;
theta2 = 0:angle2/100:angle2;
theta3 = 0:angle3/100:angle3;
L = d2:(d3-d2)/100:d3;
framemax = 100;
M=moviein(framemax);
set(gcf,'Position',[100 100 640 480]);
for k = 1:framemax
T1=RobotConv(theta1(k), d1, 0, al1);
T2=RobotConv(theta2(k), d2, 0, al2);
T3=RobotConv(theta3(k), d3, 0, al3);
T4=RobotConv(0, L(k), 0, 0);
p0 = [0 0 0];
p1 = RobotPosition(T1);
p2 = RobotPosition(T1*T2);
p3 = RobotPosition(T1*T2*T3);
p4 = RobotPosition(T1*T2*T3*T4);
figure(1)
X = [p0(1) p1(1) p2(1) p3(1) p4(1)];
Y = [p0(2) p1(2) p2(2) p3(2) p4(2)];
Z = [p0(3) p1(3) p2(3) p3(3) p4(3)];
subplot(221),plot3(X,Y,Z,'o-')
axis([-10 10 -10 10 0 10]);
grid
subplot(222),plot(X,Y,'o-')
axis([-10 10 -10 10]);
title('X-Y')
grid
subplot(223),plot(X,Z,'o-')
axis([-10 10 0 10]);
title('X-Z')
grid
subplot(224),plot(Y,Z,'o-')
axis([-10 10 0 10]);
title('Y-Z')
grid
M(k) = getframe(gcf);
end

Geoff Hayes on 7 Mar 2020
Ryan - if line 40 is
T4=RobotConv(0, L(k), 0, 0);
then the error message must be because of the index k into array L. Since array L is only used here and initialized at line
L = d2:(d3-d2)/100:d3;
then I suspect that L is an empty array. You can confirm this by putting a break point at this line and using the MATLAB debugger to step through the code. Perhaps your choices of d2 and d3 are invalid here.

G A on 7 Mar 2020
lengths of L and framemax must be equal
try
L = d2:(d3-d2)/100:d3;
framemax = length(L);