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

52 ビュー (過去 30 日間)
Justin Delmo 2020 年 5 月 11 日

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,
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 件のコメント表示非表示 2 件の古いコメント
Stephen23 2020 年 5 月 12 日

"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.

サインインしてコメントする。

### 回答 (1 件)

Shivani Dixit 2021 年 6 月 9 日
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

サインインしてコメントする。

R2020a

### Community Treasure Hunt

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

Start Hunting!