how to use a variable created inside the for loop, outside the loop?

My code:
path11='C:\Users\Avani\Documents\MATLAB\sift_COREL_101';
eu_files=dir('C:\Users\Avani\Documents\MATLAB\sift_COREL_101');
eu_files1=dir('C:\Users\Avani\Documents\MATLAB\sift_event_img');
value1=1;
for abc=3:length(eu_files);
infiles=dir(fullfile('C:\Users\Avani\Documents\MATLAB\sift_COREL_101\',eu_files(abc).name));
for def=3:length(infiles)
str=strcat(path11,'\',eu_files(abc).name,'\',infiles(def).name);
s=load(str);
for row=1:217
value=1;
sum=0;
for colu=1:6
matrix(value)=s(1).Keypoint(1,colu)-Keypoint(row,colu);
value=value+1;
end
for val=1:length(matrix)
sum=sum+matrix(val);
end
finalval(row)=sum;
end
for len=1:length(finalval)
for len1=1:length(finalval)
for len2 =len1:length(finalval)
if finalval(len1)>finalval(len2)
temp=finalval(len1);
finalval(len1)=finalval(len2);
finalval(len2)=temp;
end
end
end
end
end
if(finalval(1)>0.3 && finalval(1)<0.7)
finalmatrix(value1)=finalval(1);
finalmatrix(value1).label=eu_files(abc);
finalmatrix(value1).class='COREL_101';
value1=value1+1;
end
end

3 Comments

It is not a good idea to use a variable named "sum" !
Which variable do you want to use outside of a for loop? Which for loop is the variable in? Why can't you use it outside? Have you tried?
i have to use finalmatrix outside the for loop.. the variable is in for def=3:length(...). yes sir i have tried using it outside the loop.. an error occured...

Sign in to comment.

Answers (2)

OK, and why can't you use finalmatrix outside the loop? What happens when you try?

1 Comment

an error occurs... the err is: Undefined function or variable "finalmatrix".

Sign in to comment.

Then the problem is in this line:
if(finalval(1)>0.3 && finalval(1)<0.7)
Evidently this if statement never becomes true so finalmatrix never gets assigned.

1 Comment

Also, before the loop, initialize
finalmatrix = [];
Then if there are no files to be read, the variable will still be defined.

Sign in to comment.

Categories

Tags

Asked:

on 25 Dec 2012

Community Treasure Hunt

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

Start Hunting!