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

1 回表示 (過去 30 日間)
hemant
hemant 2016 年 10 月 12 日
コメント済み: Adam 2016 年 10 月 12 日
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 Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by