how to get element from matrix

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(get(handles.Theta_1,'string'))*pi/180;
Th_2 = str2double(get(handles.Theta_2,'string'))*pi/180;
Th_3 = str2double(get(handles.Theta_3,'string'))*pi/180;
Th_4 = str2double(get(handles.Theta_4,'string'))*pi/180;
L_1 = 10;
L_2 = 20;
L_3 = 30;
L_4 = 10;
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]);
L(4) = Link([0 0 L_4 0]);
Robot = SerialLink(L);
Robot.name = 'anosh';
Robot.plot([Th_1 Th_2 Th_3 Th_4]);
T = Robot.fkine([Th_1 Th_2 Th_3 Th_4]);
handles.Pos_X.string = num2str(floor(T(1,4)));
function Pos_X_Callback(hObject, eventdata, handles)
there is error
Index exceeds matrix dimensions.
Error in ggg>btn_forward_Callback (line 188)
handles.Pos_X.string = num2str(floor(T(1,4)));
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in ggg (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)ggg('btn_forward_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.

댓글 수: 12

ans - what can you tell us about T? From the code
T = Robot.fkine([Th_1 Th_2 Th_3 Th_4]);
handles.Pos_X.string = num2str(floor(T(1,4)));
you are assuming that T has (at least) four columns but the error message is telling you that the index exceeds the matrix dimensions...so T may not have four columns.
ans sobhi
ans sobhi 2019년 2월 18일
T is th transformation matrix of robot arm and it has fixed columns that equal four
Adam Danz
Adam Danz 2019년 2월 18일
Please copy the value of T from the command window and paste it here. I'm guessing T does not have 4 columns.
ans sobhi
ans sobhi 2019년 2월 18일
T =
1 0 0 60
0 0 -1 0
0 1 0 10
0 0 0 1
ans sobhi
ans sobhi 2019년 2월 18일
this is value of T at theta equal zero
Is this always true? Is there perhaps some cases where T is not a 4x4 matrix? Perhaps try adding the following checks before updating the position
T = Robot.fkine([Th_1 Th_2 Th_3 Th_4]);
if size(T,2) >= 4 % ensure that there at least four columns
handles.Pos_X.string = num2str(floor(T(1,4)));
else
fprintf('T does not have four columns\n ');
end
ans sobhi
ans sobhi 2019년 2월 18일
but in all cases and all values the matrix has four columns
According to the error message, at least sometimes T does not have 4 columns. I would add an sanity check similar to what Geoff suggested. If you put this conditional statement in your code just prior to the line causing error, you'll know if T is not the expected size.
T = Robot.fkine([Th_1 Th_2 Th_3 Th_4]);
if size(T,2) < 4
error('T does not have at least 4 columns.')
end
handles.Pos_X.string = num2str(floor(T(1,4)));
After doing so, run your code and I bet you'll see the error message indicated above.
If you do not see the error message indicated above, there's something fishy going on.
  • Perhaps 'floor' is a global variable and you're trying to index it. (unlikely but something to check)
  • The same could be true for 'num2str' (again, unlikely).
ans sobhi
ans sobhi 2019년 2월 20일
i solved it by this code
T = T.T;
set(handles.Pos_X,'string',T(1,4));
set(handles.Pos_Y,'string',T(2,4));
set(handles.Pos_Z,'string',T(3,4));
you many want to rename the T in
T = Robot.fkine([Th_1 Th_2 Th_3 Th_4]);
to avoid confusion with the field that you are accessing of the same name.
Adam Danz
Adam Danz 2019년 2월 20일
This suggests that T was not originally a matrix with 4 columns but instead, a structure. When you tried to get data from the 4th "column" of a structure, you got the error. 190220 084433-128.151.171.191 - Remote Desktop Connection.jpg
Arianna Arianna Clemente
Arianna Arianna Clemente 2021년 3월 24일
Hi, have you managed to run also the inverse kinematics?

댓글을 달려면 로그인하십시오.

답변 (1개)

Adam Danz
Adam Danz 2019년 2월 20일
편집: Adam Danz 2019년 2월 25일

0 개 추천

Here's a summary of the comments under the question so that this question is marked as answered.
The variable "T" is a structure but was being accessed as if it were a matrix.
Here's a replication of the error
T.T1 = magic(4); % a 4x4 matrix stored in field 'T1' of structure "T"
T(1,4)
Index exceeds matrix dimensions. %matlab 2017b
Index in position 2 exceeds array bounds (must not exceed 1). %Matlab 2018a
Here's the solution
T.T1(1,4)
ans =
13

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

릴리스

R2017a

질문:

2019년 2월 18일

댓글:

2021년 3월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by