9

Is there anyway to declare variables immune to clear all in MatLab? One solution I thought of was saving the variables and reopening them whenever I need them. Can anyone think of a more elegant solution?

EDIT: Let me explain my problem a bit more thouroughly, which I should have done in the first place; sorry for that.

I have to run a few routines using some "black box" intermediate code (some of which may be mex files). It would be good to assume that I cannot dwell into these codes. I could alter some of them, but that would be costly; for example, I don't know where the clear all is happening. I know I may be asking for too much, but you never know.

braX
  • 11,506
  • 5
  • 20
  • 33
user191919
  • 724
  • 1
  • 9
  • 27
  • 1
    Can you edit your question a bit to provide some insight as to why you're doing this, i.e., what is the actual use case? – horchler Apr 02 '15 at 22:38
  • 3
    Put your blackbox routines inside a function, output the stuff that you want. Voilà. Limited variable lifetime. – knedlsepp Apr 02 '15 at 23:01

3 Answers3

12

Instead of protecting variables, consider using clearvars with the -except flag. The use of clear all should be avoided anyway, except you really need to clear ALL.

clearvars -except v1 v2 ... clears all variables except for those specified following the -except

This answer/question can give you further inspiration.


Usage:

a = 1;
b = 2;
c = 3;

vars2keep = {'a','b'}
clearvars('-except',vars2keep{:})

or

clearvars -except a b

and who will return:

Your variables are:

a  b  
Community
  • 1
  • 1
Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
5

You can't protect individual variables, but you can use mlock to prevent an M-file function or mex function from being cleared, and any persistent variables defined within.

clear all is really a convenience when one is using the Command Window directly or when writing quick scripts. It does a lot more than just clear variables. It's not a substitute for understanding how your code works or using functions to limit variable scope. If you have a large array that is no longer used, you can explicitly tell Matlab to clear it to save memory. I'd bet what you're actually trying to do could be solved by re-thinking the structure of your code.

horchler
  • 18,384
  • 4
  • 37
  • 73
2

First of all, you should use local variables wherever possible. If someone clears the base workspace it does not matter for these variables:

function yourcode()
x=1
evilblackbox()
%x is still here
disp(x)
end


function evilblackbox()
clear all
end

There is a ugly workaround, but I really recommend not to use it. You will end up with code which requires restarting matlab whenever you exit the debugger at a wrong location, it throws an exception or similar stupid stuff.

function r=crcontainer(field,data)
persistent X
mlock
if exist('data','var')
    X.(field)=data;
end
r=X.(field);
end

To put a variable in it, use crcontainer('name',3), to read it use crcontainer('name')

Daniel
  • 36,610
  • 3
  • 36
  • 69