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
Walter Roberson
2012년 12월 25일
It is not a good idea to use a variable named "sum" !
Image Analyst
2012년 12월 25일
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?
답변 (2개)
Image Analyst
2012년 12월 25일
0 개 추천
OK, and why can't you use finalmatrix outside the loop? What happens when you try?
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
Walter Roberson
2012년 12월 25일
Also, before the loop, initialize
finalmatrix = [];
Then if there are no files to be read, the variable will still be defined.
카테고리
도움말 센터 및 File Exchange에서 Spatial Search에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!