MATLAB: I am getting an error “Matrix index is out of range for deletion” Please help

MATLABsvm

I am get error like this
Matrix index is out of range for deletion.
Error in angkaaa (line 19)
huruflist(1) = [];
Im use regionprops (eccentricity, area, perimeter) and use svm multi class for clasification
This my code
close all
clear all
e = imread('E:\Latihan MATLAB\Project_UAS\Training\huruf-i\4.jpg');
figure, imshow(e);
t = graythresh(e);
imbw_otsu = im2bw(e,t);
figure, imshow(imbw_otsu);
se = strel('disk',1);
citra_close = imclose(e,se);
figure, imshow(citra_close);
imbw_manual = im2bw(citra_close, 115/255);
figure, imshow(imbw_manual);
imbw_manual3 = 1 - imbw_manual;
figure, imshow(imbw_manual3);
traindir = 'Training';
testdir = 'Testing';
huruflist = dir(traindir);
huruflist(1) = [];
huruflist(1) = [];
huruflist = {huruflist.name}';
test_data = [];
for i=1:size(huruflist,1)
train_kelas = huruflist{i};
imglist = dir([traindir '\' train_kelas]);
imglist(1) = [];
imglist(1) = [];
imglist = {imglist.name}';
for j=1:size(imglist,1)
imgname = imglist{j};
iminput = imread([traindir '\' train_kelas '\' imgname]);
imgray = rgb2gray(iminput);
s = regionprops(imbw, 'Eccentricity', 'Area', 'Perimeter');
eccentricity = cat(1, s.Eccentricity);
area = cat(1, s.Area);
perimeter = cat(1, s.Perimeter);
rasio_luaskeliling = area / perimeter;
test_data = [test_data; {train_kelas}, imgname, rasio_luaskeliling];
end
end
xlswrite('train_features.xlsx', test_data, 1, 'A1');
testlisthuruf = dir(testdir);
testlisthuruf(1) = [];
testlisthuruf(1) = [];
testlisthuruf = {testlisthuruf.name}';
testimgdir = 'Test Images';
if ~exist(testimgdir)
mkdir(testimgdir);
end
test_data = [];
for i=1:size(testlist,1)
imgname = testlist{i};
test_class = imgname(1:end-4);
iminput = imread([testdir '\' imgname]);
imgray = rgb2gray(iminput);
s = regionprops(imbw, 'Eccentricity', 'Area', 'Perimeter');
eccentricity = cat(1, s.Eccentricity);
area = cat(1, s.Area);
perimeter = cat(1, s.Perimeter);
rasio_luaskeliling = area / perimeter;
test_data = [test_data; {train_kelas}, imgname, rasio_luaskeliling];
end
[num, raw] = xlsread('train_features.xlsx');
trainX = num(:,:);
trainY = raw(:,1);
testX = cell2mat(test_data(:,2:end));
testY = test_data(:,1);
categories = {'huruf-a';'huruf-e';'huruf-i';'huruf-o';'huruf-u'};
result = cell(size(testY));
numClasses = size(categories,1);
for i=1:numClasses
G1vAll=(strcmp(trainY,categories(i)));
models(i) = svmtrain(trainX, G1vAll, 'kernel_function', 'linear');
end
for i=1:size(testX,1)
for j=1:numClasses
if(svmclassify(models(j),testX(i,:)))
break;
end
end
result(i) = categories(j);
end
accuracy = 0;
for i=1:size(result,1)
if (strcmp(result(i),testY(i)))
accuracy = accuracy + 1;
end
end
[gr_w gr_h] = size(testY);
accuracy = (accuracy / gr_w)*100;
disp(['Akurasi = ' num2str(accuracy) '%']);
And i want result like this.
>> example
Akurasi = 100%
Thank you so much before.

Best Answer

In this section,
huruflist = dir(traindir);
huruflist(1) = [];
huruflist(1) = [];
it's likely that the directory in traindir does not exist and dir() is returning an empty structure array. When you try to delete the first element, you get the error message indicating that the index value of 1 is "out of range".
To test that the directory exists,
if exist(traindir,'dir')~=7
error('Directory does not exist: %s', traindir)
end
To look at the size of the dir() output,
size(huruflist)
To throw an error when the directory could not be found
if isempty(huruflist)
error('Problem accessing directory: %s', traindir)
end
Related Question