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

1 次查看(过去 30 天)
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

采纳的回答

KSSV
KSSV 2020-11-9
编辑:KSSV 2020-11-9
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by