while using for loop in following function some blank cells are come into output matrix like [ ] [ ] [ ]. I dont understand where i wrong in for loop

1 view (last 30 days)
function build_db(ICount, JCount)
p=0;
path = 'E:\MATLAB\R2016b\bin\img\PCA\my_PhD_Programs\minuite\FVC2002\DB1_B\'
for i=1:ICount % 10
for j=1:JCount % 8
filename=[path,num2str(i) '_' num2str(j) '.bmp'];
img = imread(filename);
img = imresize(img,[374 388]);
p=p+1;
if ndims(img) == 3;
img = rgb2gray(img);
end % colour image
disp(['extracting features from ' filename ' ...']);
if j<4
testdata{p}=ext_vein(img,1);
else
traindata{p}=ext_vein(img,1);
end
end
end
save('db1.mat','testdata');
save('db2.mat','traindata');
end
%********************************************** Function invoked *******************
build_db(2,8); %THIS WILL TAKE ABOUT 30 MINUTES
load('db1.mat');
load('db2.mat');
testdata = testdata'; % Adjust matrix dimension
testfeature = cell2mat(testdata); % Convert cell array to matrix
traindata = traindata';
trainfeature = cell2mat(traindata);
dist = pdist2(testfeature,trainfeature,'euclidean'); % compute euclidian distance
%*****************************************OUTPUT We Get *********************************************** Testdata matrix contains following values
54x6 double
44x6 double
44x6 double
[]
[]
[]
[]
[]
44x6 double
60x6 double
38x6 double
and traindata contains following values
[]
[]
[]
50x6 double
47x6 double
38x6 double
31x6 double
43x6 double
[]
[]
[]
35x6 double
37x6 double
39x6 double
27x6 double
27x6 double

Accepted Answer

Stephen23
Stephen23 on 1 Mar 2018
Edited: Stephen23 on 1 Mar 2018
You use the variable p as an index into both the training and test data cell arrays, so when you have incremented the training data index it clearly also increments the test index, because it is the same index!
The solution is simple: replace p with two separate indices, and increment them independently:
id_test = 0;
id_train = 0;
for ...
for ...
...
if j<4
id_test = id_test+1;
testdata{id_test} = ...;
else
id_train = id_train+1;
traindata{id_train} = ...;
end
...
end
end

More Answers (1)

wisam kh
wisam kh on 12 May 2018
Hello when I try this code I got this error
Error in build_db (line 9) img = imread(filename);
how can I solve it, help me please

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!