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

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?
Avani
Avani 2012년 12월 25일
편집: Avani 2012년 12월 25일
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...

댓글을 달려면 로그인하십시오.

답변 (2개)

Image Analyst
Image Analyst 2012년 12월 25일

0 개 추천

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

댓글 수: 1

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

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2012년 12월 25일
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

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

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2012년 12월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by