MATLAB Answers

Index in position 1 exceeds array bounds (must not exceed 1)

10 views (last 30 days)
Justin Delmo
Justin Delmo on 11 May 2020
Answered: Shivani Dixit on 9 Jun 2021
Hello, I am trying to run the code but I get an error message "Index in position 1 exceeds array bounds (must not exceed 1)." The error message comes from Y(1:10) = [XYZ(2,:,:)]; and when I remove it Z(1:10) = [XYZ(2,:,:)]; gets the error message. What can I do to tackle this problem.
Here is the code:
clear all; clc; close all;
qf0 = [0 0 0];
qf1 = [0 -pi/2 pi/2];
qf2 = [pi/2 0 -pi/2];
%DH parameters of the links give,
L(1) = Link('d',0,'a',5,'alpha',0);
L(2) = Link('d',0,'a',4,'alpha',0);
L(3) = Link('d',0,'a',3,'alpha',0);
Rob = SerialLink(L,'name','3R Robot') %creates the 3R robot with the specified link lengths
%The next 2 lines calculate the trajectory from one set of angles to the next
T = jtraj(qf0,qf1,10);
T2 = jtraj(qf1,qf2,10);
T1 = T'
xyz = T1(:,4,:)
x(1:10) = [xyz(1,:,:)];
y(1:10) = [xyz(2,:,:)];
z(1:10) = [xyz(3,:,:)];
plot3(x,y,z) %Plots the trajectory of the robot's movement
q = jtraj(qf1,qf2,10);
t2 = Rob.fkine(q);
XYZ = t2(:,4,:)
X(1:10) = [XYZ(1,:,:)];
Y(1:10) = [XYZ(2,:,:)]; %having problem here
Z(1:10) = [XYZ(3,:,:)]; %and here
hold on
plot3(X,Y,Z)
title('Robot Movement Trajectory')
legend('qf0 to qf1','qf1 to qf2')
  3 Comments
Stephen
Stephen on 12 May 2020
"I tried doing this and removed the square brackets..."
Removing the square brackets will not fix the error, it just removes superfluous crud from your code.
"...but I still got an error on same line code."
Read the very first line of my previous comment. So far you have not addressed this basic issue: if you expect XYZ to have three rows, then clearly it doesn't and you will have to find out why.

Sign in to comment.

Answers (1)

Shivani Dixit
Shivani Dixit on 9 Jun 2021
The above problem of "Index in position 1 exceeds array bounds (must not exceed 1)" arises when we try to access rows/columns which have value greater than the actual dimension of the matrix/vector in our code.
In your given code , the line "Y(1:10) = [XYZ(2,:,:)];" given an error while the line just above it passes. This is because XYZ has only one row, so second and third row cannot be accessed because they do not exist. To figure out the solution you must check the line "T = jtraj(qf0,qf1,10)" because XYZ is a transpose of T. Some issue in the "jtraj(qf0,qf1,10)" might be causing this problem.
XYZ = t2(:,4,:)
X(1:10) = [XYZ(1,:,:)];
Y(1:10) = [XYZ(2,:,:)]; %having problem here
Z(1:10) = [XYZ(3,:,:)]; %and here
hold on

Community Treasure Hunt

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

Start Hunting!