Clearing Global variables... except for.
31 ビュー (過去 30 日間)
古いコメントを表示
Hey all,
I wish to clear all my global variables apart from two. So I've generated a cell array of strings that contain all my global variables and removed the two I wish to save:
globals = who('global');
globals = globals(~strcmpi(globals,'waitGUI'));
globals = globals(~strcmpi(globals,'decayFig'));
This is where I'm stuck. How do I clear all the variables who's names of the variables are strings within the cell array "globals"? Thanks!
Trevor
0 件のコメント
回答 (4 件)
Daniel Shub
2012 年 10 月 11 日
編集済み: Daniel Shub
2012 年 10 月 11 日
To remove the variables locally
clear(globals{:})
to remove them globally
clear('global', var{:})
You should also look into the regexp support of clear and the -except flag.
clear -global -except waitGUI decayFig
2 件のコメント
Daniel Shub
2012 年 10 月 11 日
Sorry I thought you wanted to do it locally. To do it globally is essentially the same, except you need the global flag. You really should read the documentation of clear, since there are a number of better ways of doing this.
Jan
2012 年 10 月 11 日
A meta-answer: Avoid working with GLOBALS. There are always better solutions which do not make the debugging a trip to hell.
0 件のコメント
Image Analyst
2012 年 10 月 11 日
Here's one way. Just save what you want into local variables, then destroy all globals, then recreate the globals you want:
% Save into local variables.
saved_waitGUI = waitGUI;
saved_decayFig = decayFig;
% Clear all globals
clear global;
% Redeclare globals
global waitGUI;
global decayFig;
% Restore the globals we wanted to keep
waitGUI = keep_waitGUI;
decayFig = keep_decayFig;
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Smoothing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!