Index exceeds the number of array elements. Index must not exceed 1 error
2 views (last 30 days)
Show older comments
I have a guide gui thingy that I am trying to build to calculate triangle dimensions base on user input of angles and sides.
I have in that folder a functon to do the calculation:
function [TS1,TS2,TS3,TA1,TA2,TA3,H1,H2,H3,area] = triangleDimsSAS(TS1,TS2,TA3)
%calculates various triangle dimesions base on 2 sides and an angle
TS3=sqrt(TS2.^2 + TS1.^2 - 2 * TS2 * TS1 * cosd(TA3));
TA2 = acosd((TS3.^2 + TS1.^2 - TS2.^2)/(2 * TS3 * TS1));
TA1 = acosd((TS3.^2 + TS2.^2 - TS1.^2)/(2 * TS3 * TS2));
area = (TS1 * TS2 *sind(TA3))/2;
H1 = 2 * area/TS1;
H2 = 2 * area/TS2;
H3 = 2 * area/TS3;
%x = [0,TS3,sqrt(TS2.^2-H3.^2),0];
%y = [0,0,H3,0];
end
And in the guide code I have the following on a pushbutton object, where I call that function:
function calcBtn_Callback(hObject, eventdata, handles)
% hObject handle to calcBtn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
sideA = str2double(get(handles.sideA,'String'));
sideB = str2double(get(handles.sideB,'String'));
sideC = str2double(get(handles.sideC,'String'));
angleA = str2double(get(handles.angleA,'String'));
angleB = str2double(get(handles.angleB,'String'));
angleC = str2double(get(handles.angleC,'String'));
if (sideA>0 && sideB>0 && angleC>0) || (sideB>0 && sideC>0 && angleA>0) || (sideB>0 && sideA>0 && angleB>0)
if (sideA>0 && sideB>0 && angleC>0)
triDimvec = triangleDimsSAS(sideA,sideB,angleC);
x = [0,triDimvec(3),(sqrt((triDimvec(2).^2)-(triDimvec(9).^2))),0];
y = [0,0,triDimvec(9),0];
infoTxt1 = sprintf('Side A: %0.2f; \n Side B: %0.2f; \n Side C: %0.2f; \n ', triDimvec(1), triDimvec(2), triDimvec(3));
infoTxt2 = sprintf('Angle A: %0.2f; \n Angle B %0.2f; \n Angle A: %0.2f; \n',triDimvec(4), triDimvec(5), triDimvec(6));
infoTxt3 = sprintf('Height A: %0.2f; \n Height B: %0.2f; \n Height C: %0.2f;\n Area: %0.2f;\n',triDimvec(7), triDimvec(8), triDimvec(9), triDimvec(10));
infoTxt4 = sprintf('Vertex Coordinates:\n ');
infoTxt5 = sprintf('A[%0.2f,%0.2f]; B[%0.2f,%0.2f]; C[%0.2f,%0.2f];',x(1),y(1),x(2),y(2),x(3),y(3));
set(handles.calcInfo, 'String',{infoTxt1,infoTxt2,infoTxt3,infoTxt4,infoTxt5});
plot(x,y);
axis padded;axis equal
else
%do something 1
end
else
%do something 2
end
Try to run it ends with the following errors:
Index exceeds the number of array elements. Index must not exceed 1.
Error in ATCV1>calcBtn_Callback (line 250)
x = [0,triDimvec(3),(sqrt((triDimvec(2).^2)-(triDimvec(9).^2))),0];
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in ATCV1 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)ATCV1('calcBtn_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
If I run it all from whithin the gui code, with in the if statement, I dont get any errors. So....what does it want? why I cannot call an element of my vector (from the return of the triangleDimsSAS function to than be used as another vector (x,y) elements or elemnts after some arithmetic manipulations?
0 Comments
Accepted Answer
VBBV
on 19 Nov 2022
Edited: VBBV
on 19 Nov 2022
This function is not returning all Ten variables as defined by fucntion output [TS1,TS2,TS3,TA1,TA2,TA3,H1,H2,H3,area]
function [TS1,TS2,TS3,TA1,TA2,TA3,H1,H2,H3,area] = triangleDimsSAS(TS1,TS2,TA3)
%calculates various triangle dimesions base on 2 sides and an angle
TS3=sqrt(TS2.^2 + TS1.^2 - 2 * TS2 * TS1 * cosd(TA3));
TA2 = acosd((TS3.^2 + TS1.^2 - TS2.^2)/(2 * TS3 * TS1));
TA1 = acosd((TS3.^2 + TS2.^2 - TS1.^2)/(2 * TS3 * TS2));
area = (TS1 * TS2 *sind(TA3))/2;
H1 = 2 * area/TS1;
H2 = 2 * area/TS2;
H3 = 2 * area/TS3;
%x = [0,TS3,sqrt(TS2.^2-H3.^2),0];
%y = [0,0,H3,0];
end
its only returning TS3 TA2 TA1 area H1 H2 H3 only 7 varables. but in this line below
x = [0,triDimvec(3),(sqrt((triDimvec(2).^2)-(triDimvec(9).^2))),0];
^^
you are accessing 9th element of triDimvec which does not exist
3 Comments
VBBV
on 19 Nov 2022
yes, you can do so such that triDimvec has all elements which you want to access in the pushbutton callback
More Answers (0)
See Also
Categories
Find more on Interactive Control and Callbacks 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!