Index in position 2 exceeds array bounds (must not exceed 1). Error can't seem to find the mistake

2 views (last 30 days)
So I recently tried changing the arrow head visuals through annotations, the following code is a modified one, and everytime I run it, it returns an 'exceed array bounds' error I already double checked everything, but I still can't find the problem as I already individually call the variables and check their array dimensions and everything check out. Any ideas why?
p = y(:,3);
o = y(:,4);
u = cos(p).*o;
v = sin(p).*o;
quiver(p,o,u,v,'r');
headWidth = 3;
headLength = 3;
LineLength = 0.03;
U = hq.UData;
V = hq.VData;
X = hq.XData;
Y = hq.YData;
hax_2 = subplot(1,2,2);
for ii = 1:length(X)
for ij = 1:length(X)
headWidth = 3;
ah = annotation('arrow',...
'headStyle','cback1','HeadLength',headLength,'HeadWidth',headWidth);
set(ah,'parent',gca);
set(ah, 'position', [X(ii,ij) Y(ii,ij) U(ii,ij) V(ii,ij)])
end
end

Accepted Answer

KSSV
KSSV on 9 Nov 2020
Edited: KSSV on 9 Nov 2020
You have to specify the dimensions of row and column of a matrix. You should use Size. You have used length and it is creating problem . Length will give you the maximum of length of row or column.
p = y(:,3);
o = y(:,4);
u = cos(p).*o;
v = sin(p).*o;
quiver(p,o,u,v,'r');
headWidth = 3;
headLength = 3;
LineLength = 0.03;
U = hq.UData;
V = hq.VData;
X = hq.XData;
Y = hq.YData;
hax_2 = subplot(1,2,2);
for ii = 1:size(X,1) % USe rows here
for ij = 1:size(X,2) %USe columns here
headWidth = 3;
ah = annotation('arrow',...
'headStyle','cback1','HeadLength',headLength,'HeadWidth',headWidth);
set(ah,'parent',gca);
set(ah, 'position', [X(ii,ij) Y(ii,ij) U(ii,ij) V(ii,ij)])
end
end

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!