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?
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 个)

OK, and why can't you use finalmatrix outside the loop? What happens when you try?
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