Old variable values in a for loop affecting the newer ones. Even after using "clearvars" command.

5 次查看(过去 30 天)
Hello, I am doing some image processing on a set of three images and the operations on all the three images are same. At the end I am saving the results on the processed image in a separate '.jpeg' file. Although I haven't attached the results but it is clear from them that the values of the variables 'peakInfo' & possibly 'pathArray' from the previous loop are somehow still in memory and leading to wrong results for the consecutive loops(code is attached below).This is happening even after I have cleared all the variables at the end of every loop. Any help would be greatly appreciated. Thank You!
data=evalin('base','data');
for i=1:3
rawImage=data{i,1};
imgArr=medfilt2(rawImage);
imgArr=imgaussfilt(imgArr,[3,2]);
peakInfo=generatePeakInfo(imgArr); % An m*4 cell matrix where 'm' changes for every iteration
pathArray=huy2(peakInfo,size(imgArr));% An n*2 cell matrix where 'n' changes for every iteration.
gap=ones(1000,5);
group=[rawImage,gap,generateMarkedPath(pathArray,rawImage,1),gap,imgArr,gap,generateMarkedPath(pathArray,imgArr,1)];
fileName=strcat('image',num2str(i),'.jpeg');
imwrite(group,fileName);
clearvars -except data
end
  1 个评论
Adam
Adam 2016-10-12
It seems far more likely there is a bug in your generatePeakInfo function or that the data is actually the same in different indices of the cell array that is causing the same peakInfo to appear on multiple times round the loop.

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2016-10-12
编辑:Jan 2016-10-12
No, this cannot be the problem. Both variables "peakInfo" and "pathArray" are overwritten in each itereation. But think twice: What will happen, if you could really clear all variables inside a loop? Shouldn't the loop counter vanish also? So simply om,it the clearvars because there is no reason to call this confusing command.
Matlab is a deterministic programming language. There is no way to keep the value of overwritten variables "somehow". There is no magic in Matlab.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by