how to use a variable created inside the for loop, outside the loop?
Show older comments
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
Walter Roberson
on 25 Dec 2012
It is not a good idea to use a variable named "sum" !
Image Analyst
on 25 Dec 2012
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?
Answers (2)
Image Analyst
on 25 Dec 2012
0 votes
OK, and why can't you use finalmatrix outside the loop? What happens when you try?
Image Analyst
on 25 Dec 2012
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
Walter Roberson
on 25 Dec 2012
Also, before the loop, initialize
finalmatrix = [];
Then if there are no files to be read, the variable will still be defined.
Categories
Find more on Spatial Search in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!