saveexcept.m 738 B

1234567891011121314151617181920212223242526272829303132
  1. function saveexcept(file,vars)
  2. % function saveexcept(file,vars)
  3. %
  4. % <file> is a string referring to a .mat file
  5. % <vars> is a variable name or a cell vector of variable names to NOT save
  6. %
  7. % save all variables that exist in the caller to <file>,
  8. % except variables named by <vars>.
  9. %
  10. % example:
  11. % x = 1; y = 2; z = 3;
  12. % saveexcept('temp.mat','z');
  13. % a = load('temp.mat')
  14. % input
  15. if ~iscell(vars)
  16. vars = {vars};
  17. end
  18. % figure out variable names
  19. varlist = evalin('caller','whos');
  20. varlist = cat(2,{varlist.name});
  21. % exclude the ones we don't want
  22. ok = cellfun(@(x) ~ismember(x,vars),varlist);
  23. varlist = varlist(ok);
  24. % save the data
  25. temp = cell2str(varlist);
  26. cmd = sprintf('save ''%s'' %s;',file,temp(3:end-2));
  27. evalin('caller',cmd);