printnice.m 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. function filenames = printnice(figs,mode,directory,prefix)
  2. % function filenames = printnice(figs,mode,directory,prefix)
  3. %
  4. % <figs> (optional) is a vector of figure numbers. default: [gcf].
  5. % <mode> (optional) is
  6. % 0 means .eps (using flags "-depsc2 -painters -r300").
  7. % [0 1] means also use the flag "-loose".
  8. % [1 n] means .png at n pixels per inch (using flags "-dpng -r(n)")
  9. % default: 0.
  10. % <directory> (optional) is the directory to temporarily change into
  11. % when writing the files. default is the current working directory.
  12. % we automatically make the directory if it doesn't exist.
  13. % <prefix> (optional) is the prefix of the output filename. the prefix
  14. % can include in it '%d' (or a variant thereof) for the figure number.
  15. % you can pass in a number and we automatically convert it using num2str.
  16. % default: '%d'.
  17. %
  18. % print figure windows to files in <directory>.
  19. % return a cell vector of the files written.
  20. %
  21. % note that if <prefix> has a directory that precedes the actual filename,
  22. % we attempt to automatically make that directory.
  23. %
  24. % history:
  25. % 2011/06/29 - temporarily change PaperPositionMode to auto before printing
  26. %
  27. % example:
  28. % figure; scatter(randn(100,1),randn(100,1),'r.'); printnice;
  29. % NOTE: removed special eps pixel mode (see old printnice.m)
  30. % SEE: figurewrite.m
  31. % input
  32. if ~exist('figs','var') || isempty(figs)
  33. figs = [gcf];
  34. end
  35. if ~exist('mode','var') || isempty(mode)
  36. mode = 0;
  37. end
  38. if ~exist('directory','var') || isempty(directory)
  39. directory = pwd;
  40. end
  41. if ~exist('prefix','var') || isempty(prefix)
  42. prefix = '%d';
  43. end
  44. if ~ischar(prefix)
  45. prefix = num2str(prefix);
  46. end
  47. olddir = pwd;
  48. mkdirquiet(directory);
  49. cd(directory);
  50. % make dir if necessary
  51. dir0 = stripfile(prefix);
  52. if ~isempty(dir0) && ~exist(dir0,'dir')
  53. mkdirquiet(dir0);
  54. end
  55. % do it
  56. filenames = {};
  57. for p=1:length(figs)
  58. fig = figs(p);
  59. % temporarily change
  60. prev = get(fig,'PaperPositionMode');
  61. set(fig,'PaperPositionMode','auto');
  62. switch mode(1)
  63. case 0
  64. filename = sprintf([prefix '.eps'],double(fig)); % the double casts fig when it is a class
  65. if length(mode) > 1
  66. print(fig,'-depsc2','-painters','-r300','-loose',filename);
  67. else
  68. print(fig,'-depsc2','-painters','-r300',filename);
  69. end
  70. case 1
  71. filename = sprintf([prefix '.png'],double(fig)); % the double casts fig when it is a class
  72. print(fig,'-dpng',['-r' num2str(mode(2))],filename); % painters, zbuffer, opengl??? what is correct?
  73. end
  74. % fprintf('wrote %s.\n',filename);
  75. % restore
  76. set(fig,'PaperPositionMode',prev);
  77. % record
  78. filenames{p} = [directory filesep filename];
  79. end
  80. cd(olddir);