MATLAB: I need help please

image processing

please I have a problem in this code i want to convert an image and to compare it with other images:
function Couleur_Callback(hObject, eventdata, handles)
global a;
disp (a);
I1=rgb2gray(a);
histo=imhist(I1);
r=dir('base');
[I c]=size(r);
j=1;
for i=3:1
ch1=r(i).name;
disp(ch1);
img=stract('base\',ch1);
disp(img);
imagetestt=imread(img);
imagetest2=rgb2gray( imagetestt);
imagetest=imhist(imagetest2);
valeur=intersectionhisto(histo,imagetest);
resulat(j)=valeur;
j=j+1;
end
resultatFinal=sort(resulat,'descend')
for i=1:j-1
X=find(resultatFinal(i)==resultat)
indice(i)=X(1);
end
for i=1:6
nomImage=stract('base\',num2str(indice(i)),'.jpg');
I=imread(nomImage);
subplot(6,1,i);
imshow(I);
end

Best Answer

olfa - look closely at your for loop
for i=3:1
Your indexing variable i begins at three and ends at one. I expect that the body of the for loop is not being executed because 3 is greater than the 1 and so resulat is not initialized. From for loop, for index=initVal:endVal — Increment the index variable from initVal to endVal by 1, and repeat execution of statements until index is greater than endVal. If you want your indexing variable to take on values of 3,2,1 then you need to use a negative step size as
for k=3:-1:1
Try the above and see what happens! Note that the above code is using k as an indexing variable since MATLAB uses i and j to represent the imaginary number. It is also good practice to initialize your variables before you begin to use them. In your case, you can initialize resulat as an array with three variables or just as an empty vector
resulat = []; % or
resulat = zeros(3,1);
for k=3:-1:1
% etc.
end