Why my GUI keeps producing error when I run it?

When I run the code, it gives me these errors. Can anyone help me solve this problem? I am using the 2019 b version.
Index at position 2 exceeds array boundary (cannot exceed 1)
Error Sample_2>btn_Forward_Callback (line 168)
handles.Pos_X.String=num2str(floor(T(1,4)));
Error gui_mainfcn (line 95)
feval(varargin{:});
Error Sample_2 (line 42)
gui_mainfcn(gui_State, varargin{:});
This is my code. Edit Rik: removed empty functions and default GUIDE code at the function start. The full original code is attached as m-file.
function varargout = Sample_2(varargin)
%standard GUIDE header, OpeningFcn and OutputFcn
% --- Executes on button press in btn_Forward.
function btn_Forward_Callback(hObject, eventdata, handles)
% hObject handle to btn_Forward (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Th_1=str2double(handles.Theta_1.String)*pi/180;
Th_2=str2double(handles.Theta_2.String)*pi/180;
Th_3=str2double(handles.Theta_3.String)*pi/180;
L_1=20;
L_2=50;
L_3=40;
L(1)=Link([0 L_1 0 pi/2]);
L(2)=Link([0 0 L_2 0]);
L(3)=Link([0 0 L_3 0 ]);
Robot=SerialLink(L);
Robot.name='Sample_2';
Robot.plot([Th_1 Th_2 Th_3]);
T = Robot.fkine([Th_1 Th_2 Th_3]);
handles.Pos_X.String=num2str(floor(T(1,4)));
handles.Pos_Y.String=num2str(floor(T(2,4)));
handles.Pos_Z.String=num2str(floor(T(3,4)));
% --- Executes on button press in btn_Inverse.
function btn_Inverse_Callback(hObject, eventdata, handles)
% hObject handle to btn_Inverse (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
PX=str2double(handles.Pos_X.String);
PY=str2double(handles.Pos_Y.String);
PZ=str2double(handles.Pos_Z.String);
L_1=20;
L_2=50;
L_3=40;
L(1)=Link([0 L_1 0 pi/2]);
L(2)=Link([0 0 L_2 0]);
L(3)=Link([0 0 L_3 0 ]);
Robot=SerialLink(L);
Robot.name='Sample';
T=[1 0 0 PX;
0 1 0 PY;
0 0 1 PZ;
0 0 0 1;]
J=Robot.ikine(T,[0 0 0],[1 1 1 0 0 0])*180/pi;
handles.Theta_1.String=num2str(floor(J(1)));
handles.Theta_2.String=num2str(floor(J(2)));
handles.Theta_3.String=num2str(floor(J(3)));
Robot.plot(J*pi/180);

2 Comments

Look at your variable "T". does it have 4 columns. from the error it appears that this variable only has 1 column.
This time I edited your question for you. Next time, please use the tools explained on this page to make your question more readable.

Sign in to comment.

 Accepted Answer

You define T like this:
Th_1=str2double(handles.Theta_1.String)*pi/180;
Th_2=str2double(handles.Theta_2.String)*pi/180;
Th_3=str2double(handles.Theta_3.String)*pi/180;
%other code cropped%
T = Robot.fkine([Th_1 Th_2 Th_3]);
That is not resulting in a 4-column matrix, as your other callback is creating. How to solve this is unclear because you haven't written any comments about what is happening.

3 Comments

Actually, when I run the forward kinmatics code in command window. The T seems to be a 4 column matrix, but if I try to take an element from the matrix, it shows the following error, suggesting T is not a 4 column matrix.
Sorry for the Chinese version.
The error states that Index at position 2 exceeds array boundary (cannot exceed 1)
'T' seems to be both a function and a varibale. If it is not expected,please use 'clear T' to remove it from the workspace.
Is it because T is not in a 4 column matrix form but a 1x1 matrix? Could you please help me solve this problem?
Your screenshot doesn't make clear if your code is a script (in which case it would be using the base workspace), or if it is a function (in which case it has its own workspace separate from the base workspace and can therefore contain a different T).
Your screenshot shows T is an SE3 object, whatever that is. It looks to me like the line we see in the editor is part of a function and generated the correct shape matrix, after which you ran the line T(1,4) from your base workspace, using a different T. The suggestion that T could be a function as well is worysome. You should not use functions with such a short and non-descriptive name.
Thanks a lot! T is actually made of 4 arrays (t, n, o, a) to form the structured matrix. It appears a 4x4 matrix, but pressing size(T) returns 1x1. I just need to specify the array in T to access the elements in the matrix. For example, I should use T.t(1,1) instead of T(1,4).

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 26 Mar 2020

Commented:

on 28 Mar 2020

Community Treasure Hunt

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

Start Hunting!