Old variable values in a for loop affecting the newer ones. Even after using "clearvars" command.
1 view (last 30 days)
Show older comments
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 Comment
Adam
on 12 Oct 2016
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.
Answers (1)
Jan
on 12 Oct 2016
Edited: Jan
on 12 Oct 2016
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.
0 Comments
See Also
Categories
Find more on Logical 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!