Why my GUI keeps producing error when I run it?
9 views (last 30 days)
Show older comments
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
Mohammad Sami
on 26 Mar 2020
Look at your variable "T". does it have 4 columns. from the error it appears that this variable only has 1 column.
Accepted Answer
Rik
on 26 Mar 2020
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
Rik
on 28 Mar 2020
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.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!