Clearing everything except variables

Is there a convenient way to do a "clear all" but not remove all the variables? I want to clear everything that isn't a variable. I have a spontaneous problem in 2010a where something gets cached and disrupts my ADODB database connection. Clear All will fix it, but it also clears my variables. Thanks -Greg

답변 (6개)

Sean de Wolski
Sean de Wolski 2011년 4월 27일

2 개 추천

And probably faster that save and load
S = struct('var1',var1); %etc
setappdata(0,'S',S);
clear all;
S = getappdata(0,'S');
var1 = S.var1;
You could save them directly/dynamically as well (i.e. not in a struct, the struct is just nice for compactness)

댓글 수: 10

Andrew Newell
Andrew Newell 2011년 4월 27일
What a curious approach! I would recommend adding the line
rmaddpdata(0,'S')
after the above.
Sean de Wolski
Sean de Wolski 2011년 4월 27일
Yes; Good Call!
I use setappdata/getappdata to pass +1GB variables between functions. It's much faster than calling them directly as inputs to a function, I get full use of clear (without losing those two variable since I pretty much need them to be persistent, while being faster than global or persistent.
Andrew Newell
Andrew Newell 2011년 4월 27일
Strange that it should be so fast. It must be using a pointer of some kind.
Fangjun Jiang
Fangjun Jiang 2011년 4월 27일
Is this approach practical if there are "a gig of variables" in the Matlab base workspace? Would it cause memory problem if every variable needs to be copied to a structure?
Gregory Jones
Gregory Jones 2011년 4월 27일
Thanks--I will try this.
BTW clear all works to fix my caching & DB problem, the
>> clear functions import java mex
doesn't work.
Sean de Wolski
Sean de Wolski 2011년 4월 27일
Fangjun: The structure was just for compactness and to make sure that you don't accidentally overwrite something important in the root directory. You can write the variables to appdata directly.
For some reason, perhaps a pointer like Andrew suggested, it is orders of magnitude faster.
Andrew Newell
Andrew Newell 2011년 4월 27일
The possibility of accidentally doing harm is one thing that worries me about this approach.
Fangjun Jiang
Fangjun Jiang 2011년 4월 28일
Sean, what if I have hundreds of variables in the base workspace? Does it require hundreds of lines of "setappdata()" and "getappdata()"? That is why I ask if it is practical. I can see its advantage if I have less number of variables but each variable has large size.
Sean de Wolski
Sean de Wolski 2011년 5월 2일
@Fangjun: If you have hundreds of variables then you're doing something else wrong. Store them in a cell array or a struct and then use appdata once.
@Andrew: That's why I typically use underscores &| numbers.
Fangjun Jiang
Fangjun Jiang 2011년 5월 2일
I am curious to know what final approach Gregory Jones took to solve his original problem. I sense he might have many variables. "clear all" works but we couldn't seem to eliminate its undesired side effect (clear variables). The best approach, I would think, is to find out the exact and exclusive effects of "clear all". Is it the combination of "clear variables", "clear functions", "clear mex", "clear import" etc? Then, Greg needs to find out which one of these "clear" commands actually did the trick.

댓글을 달려면 로그인하십시오.

Arnaud Miege
Arnaud Miege 2011년 4월 27일

0 개 추천

Have you tried the following?
clear classes
clear functions
clear import
clear mex
Arnaud

댓글 수: 2

Gregory Jones
Gregory Jones 2011년 4월 27일
clear classes will clear variables, I think!
Arnaud Miege
Arnaud Miege 2011년 4월 27일
You're right, sorry!!

댓글을 달려면 로그인하십시오.

Andrew Newell
Andrew Newell 2011년 4월 27일

0 개 추천

You could try
clear functions import java mex
Paulo Silva
Paulo Silva 2011년 4월 27일

0 개 추천

save MyWorkVars
matlabrc
load MyWorkVars
delete(MyWorkVars)

댓글 수: 2

Gregory Jones
Gregory Jones 2011년 4월 27일
The load-and-save are not very practical if I am working with a gig of variables, since the process is quite slow.
Sean de Wolski
Sean de Wolski 2011년 4월 27일
Hence, setappdata/getappdata!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

질문:

2011년 4월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by