MATLAB Answers

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

2,368 views (last 30 days)
Ramesh Bala
Ramesh Bala on 3 Jul 2018
Commented: Walter Roberson on 1 Sep 2021 at 5:17
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 (5)

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
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
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
Edited: Walter Roberson on 19 Jun 2021
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.


Zain Achmad
Zain Achmad on 31 Aug 2021 at 21:46
can anyone help me to solve this?
Index in position 2 exceeds array bounds.
Error in klasifikasi_svm (line 82)
PC1 = score_latih(:,1);
-------------------------------------------------------------------------------
nama_folder = 'data latih';
nama_file = dir(fullfile(nama_folder,'*.jpg'));
jumlah_file = numel(nama_file);
% inisialisasi variabel ciri_latih
ciri_latih = zeros(jumlah_file,4);
for n = 1:jumlah_file
% membaca citra RGB
Img = imread(fullfile(nama_folder,nama_file(n).name));
% konversi citra RGB menjadi grayscale
Img_gray = rgb2gray(Img);
% konversi citra grayscale menjadi biner
bw = im2bw(Img_gray,graythresh(Img_gray));
% operasi morfologi
bw = imcomplement(bw);
bw = imfill(bw,'holes');
bw = bwareaopen(bw,100);
% ekstraksi ciri warna HSV
HSV = rgb2hsv(Img);
H = HSV(:,:,1);
S = HSV(:,:,2);
V = HSV(:,:,3);
H(~bw) = 0;
S(~bw) = 0;
V(~bw) = 0;
Hue = sum(sum(H))/sum(sum(bw));
Saturation = sum(sum(S))/sum(sum(bw));
Value = sum(sum(V))/sum(sum(bw));
% ekstraksi ciri ukuran
Area = sum(sum(bw));
% mengisi hasil ekstraksi ciri pada variabel ciri_latih
ciri_latih(n,1) = Hue;
ciri_latih(n,2) = Saturation;
ciri_latih(n,3) = Value;
ciri_latih(n,4) = Area;
end
% standarisasi data
[ciri_latihZ,muZ,sigmaZ] = zscore(ciri_latih);
% pca
[coeff,score_latih,latent,tsquared,explained] = pca(ciri_latihZ);
% inisialisasi variabel kelas_latih
kelas_latih = cell(jumlah_file,1);
% mengisi nama2 sayur pada variabel kelas_latih
for k=1:300
kelas_latih{k} = 'Angry';
end
for k=301:600
kelas_latih{k} = 'Disgust';
end
for k=601:900
kelas_latih{k} = 'Fear';
end
for k=901:1200
kelas_latih{k} = 'Happy';
end
for k=1201:1500
kelas_latih{k} = 'Neutral';
end
for k=1501:1800
kelas_latih{k} = 'Sad';
end
for k=1801:2100
kelas_latih{k} = 'Surprise';
end
% ekstrak PC1 & PC2
PC1 = score_latih(:,1);
PC2 = score_latih(:,2);
PC3 = score_latih(:,3);
PC4 = score_latih(:,4);
PC5 = score_latih(:,5);
% klasifikasi menggunakan
Mdl = fitcecoc([PC1,PC2,PC3,PC4,PC5],kelas_latih);
% menyimpan variabel-variabel hasil pelatihan
save hasil_pelatihan Mdl muZ coeff sigmaZ
-----------------------------------
  3 Comments
Walter Roberson
Walter Roberson on 1 Sep 2021 at 5:17
What is the value of
jumlah_file
size(ciri_latih)
size(score_latih)
I suspect that your current directory is not the directory that the folder 'data latih' is inside. I suspect that your current directory is 'data latih' itself rather than the directory that contains 'data latih' as your code requires.

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!