loadexcept.m 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. function f = loadexcept(file,vars,mode)
  2. % function f = loadexcept(file,vars,mode)
  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 load
  6. % <mode> (optional) is
  7. % 0 means load <file> into the base workspace (as well as into struct <f>)
  8. % 1 means load <file> into struct <f>
  9. % Default: 0.
  10. %
  11. % load <file>, excluding variables named by <vars>.
  12. %
  13. % example:
  14. % x = 1; y = 2;
  15. % save('temp.mat','x','y');
  16. % clear x y;
  17. % loadexcept('temp.mat','x')
  18. % ~exist('x','var')
  19. % exist('y','var')
  20. % input
  21. if ~exist('mode','var') || isempty(mode)
  22. mode = 0;
  23. end
  24. if ~iscell(vars)
  25. vars = {vars};
  26. end
  27. % figure out variable names
  28. varlist = whos('-file',file);
  29. varlist = cat(2,{varlist.name});
  30. % exclude the ones we don't want
  31. ok = cellfun(@(x) ~ismember(x,vars),varlist);
  32. varlist = varlist(ok);
  33. % load in the data
  34. f = load(file,varlist{:});
  35. % deal
  36. switch mode
  37. case 0
  38. % assign to caller's workspace
  39. for p=1:length(varlist)
  40. assignin('base',varlist{p},f.(varlist{p}));
  41. end
  42. case 1
  43. end