function saveexcept(file,vars) <file> is a string referring to a .mat file <vars> is a variable name or a cell vector of variable names to NOT save save all variables that exist in the caller to <file>, except variables named by <vars>. example: x = 1; y = 2; z = 3; saveexcept('temp.mat','z'); a = load('temp.mat')
0001 function saveexcept(file,vars) 0002 0003 % function saveexcept(file,vars) 0004 % 0005 % <file> is a string referring to a .mat file 0006 % <vars> is a variable name or a cell vector of variable names to NOT save 0007 % 0008 % save all variables that exist in the caller to <file>, 0009 % except variables named by <vars>. 0010 % 0011 % example: 0012 % x = 1; y = 2; z = 3; 0013 % saveexcept('temp.mat','z'); 0014 % a = load('temp.mat') 0015 0016 % input 0017 if ~iscell(vars) 0018 vars = {vars}; 0019 end 0020 0021 % figure out variable names 0022 varlist = evalin('caller','whos'); 0023 varlist = cat(2,{varlist.name}); 0024 0025 % exclude the ones we don't want 0026 ok = cellfun(@(x) ~ismember(x,vars),varlist); 0027 varlist = varlist(ok); 0028 0029 % save the data 0030 temp = cell2str(varlist); 0031 cmd = sprintf('save ''%s'' %s;',file,temp(3:end-2)); 0032 evalin('caller',cmd);