MATLAB Answers

Index in position 2 exceeds array bounds (must not exceed 1) ??

3,292 views (last 30 days)
Kaleesh Bala
Kaleesh Bala on 3 Jul 2018
Commented: cuong nguyen ngco about 8 hours ago
Why does the following function produce the error "Index in position 2 exceeds array bounds (must not exceed 1)"?
function test2
close all
prompt ={'Enter the x1 value: ','Enter the y1 value: ','Enter the x2 value: ','Enter the y2 value: '};
Is = inputdlg(prompt);
Is = str2double(Is);
%Eucleidan distance
D = sqrt((Is(1,2)-Is(1,1))^2 + (Is(1,4)-Is(1,3))^2);
fprintf ('the distance value obtained is %d',D)
end

Accepted Answer

Guillaume
Guillaume on 3 Jul 2018
Edited: MathWorks Support Team on 27 Feb 2020
The error occurs because the vector “Is” is a 4-by-1 column vector (not a 1-by-4 row vector) so that the valid element positions are (1,1), (2,1), (3,1), and (4,1). If you try to access any index value beyond 1 in the 2nd position, you will encounter the error.
  3 Comments
Walter Roberson
Walter Roberson on 5 May 2021
When you use xlsread(), the first output is trimmed down to remove all leading and trailing rows and columns that are all NaN after conversion to numbers. Typically that happens for header rows and for leading or trailing text columns. You first column probably has text in it, so it became all NaN when converted to numbers, and then got discarded. That would have thrown off your indexing.
We recommend that you switch to readtable().

Sign in to comment.

More Answers (4)

sachin bharadwaj
sachin bharadwaj on 5 Nov 2020
Index in position 2 exceeds array bounds (must not exceed 1).
Error in fatlabexam (line 17)
g=PP(:,1)*h(1)+PP(:,2)*h(2);
why is it showing error?

Tuyet Nhung
Tuyet Nhung on 20 May 2021
Index in position 2 exceeds array bounds.
Error in m20 (line 7)
t = ndata(:,1); Vab = ndata(:,2); VR = ndata(:,3); Ir = VR/45;
Here is the code:
function m20
clc
h = figure;
set(h,'position',[10,10,600,680],'numbertitle','off','color', 'w')
ndata = xlsread('..\PD\Vab_Vr\20.csv');
R = 45;
t = ndata(:,1); Vab = ndata(:,2); VR = ndata(:,3); Ir = VR/45;
%Ve Vcm
subplot( 2,1,1), plot(t,Vab ,'color',[1,0.5,0]); grid,
ylabel('Mag (V)','fontname','vni-times','fontsize',15),
title('a) V_a_b','fontname','vni-times','fontsize',20),
xlabel('Time (seconds)','fontname','vni-times','fontsize',15)
%Ve Van
subplot( 2,1,2), plot(t,Ir ,'color',[1,0,0.8]); grid, axis([-0.03,0.03,-2,2])
ylabel('Mag (V)','fontname','vni-times','fontsize',15),
title('b) I_R','fontname','vni-times','fontsize',20),
xlabel('Time (seconds)','fontname','vni-times','fontsize',15)
Can you help me to solve the problem?
  4 Comments

Sign in to comment.


Nurliyana Hadi
Nurliyana Hadi on 6 Jun 2021 at 12:58
can anyone help me to solve this??
Index in position 2 exceeds array bounds.
Error in Example3_cmp (line 15)
plot(a(:,1)-1,a(:,2));
--------------------------------------------------------------
param =34473.8;
plot(a(:,1)-1,a(:,2))
hold on
plot(node(:,1)-1,(nodefc1(:,2)-node(:,2))/(0.01*param),'b--','linewidth',2)
plot(node(:,1)-1,(nodefc2(:,2)-node(:,2))/(0.001*param),'g','linewidth',2)
plot(node(:,1)-1,(nodefc3(:,2)-node(:,2))/(0.0001*param),'k','linewidth',2)
plot(node(:,1)-1,(nodefc4(:,2)-node(:,2))/(0.00001*param),'r:','linewidth',2)
legend('DDM','FFD 0.01','FFD 0.001','FFD 0.0001','FFD 0.00001')
xlabel('Time [sec]')
ylabel('\partialu_6/\partialfc')
set(get(gca,'ylabel'),'Fontsize',14)
set(get(gca,'xlabel'),'Fontsize',14)
set(gca,'Fontsize',14)
figure (2)
load node.out
b = load('node_sens6.out');
load nodeE1.out
load nodeE2.out
load nodeE3.out
%load nodeE4.out
param =2.1e8;
plot(b(:,1)-1,b(:,2))
hold on
plot(node(:,1)-1,(nodeE1(:,2)-node(:,2))/(0.0005*param),'b--','linewidth',2)
plot(node(:,1)-1,(nodeE2(:,2)-node(:,2))/(0.0003*param),'g','linewidth',2)
plot(node(:,1)-1,(nodeE3(:,2)-node(:,2))/(0.0001*param),'k','linewidth',2)
%plot(node(:,1)-1,(nodeE4(:,2)-node(:,2))/(0.00001*param),'r:')
  1 Comment
Walter Roberson
Walter Roberson on 19 Jun 2021 at 1:55
We have no information about what a or b are or how big they are or where they came from.

Sign in to comment.


cuong nguyen ngco
cuong nguyen ngco on 19 Jun 2021 at 0:28
Edited: Walter Roberson on 19 Jun 2021 at 3:54
can you help me for error Index in position 2 exceeds array bounds (must not
exceed 1) with trains = data(:,Tr_ind);
%main function main
load('ORL_32x32.mat')%importing face data
%%split datta
data=load('ORL_32x32.mat');
label=unique(gnd);
TrainNum=5;
Tr_ind=[];
Te_ind=[];
for i=1:length(label)
tempind=find(gnd==label(i));
Tr_ind=[Tr_ind,tempind(1:TrainNum)];
Te_ind=[Te_ind,tempind(TrainNum+1:end)];
end
%%label and data
Train_label=gnd(Tr_ind);
Test_label=gnd(Te_ind);
Trains=data(:,Tr_ind);
  6 Comments

Sign in to comment.

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!