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 th
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')