save a plot in png, eps, and fig formats
February 12, 2015 at 01:07 AM | categories: matlab, octave, workflow | View Comments
When you save a figure, what you really want to save is - several formats, a fig file, all relevant data that is needed to reconstruct the fig, and a README that tells you what is there. That's the purpose of my savefigs :
1: % purpose : save a figure in png,eps, and fig formats 2: % syntax : savefigs(filename,readme_text,data_str) 3: % filename - file name without any suffix (savefigs does not check this, so 4: % if you mistakenly set filename="stam.fig", the output files will 5: % be stam.fig.fig, stam.fig.eps, stam.fig.png) 6: % readme_text - a string that describes the figure, and the data. 7: % data_str - a data structure that contains all needed info in 8: % order to reconstruct the figure 9: % 10: % see also: print, hgsave 11: 12: 13: % Copyright 2013 Avi Gozolchiani (http://tiny.cc/avigoz) 14: % This program is free software: you can redistribute it and/or modify 15: % it under the terms of the GNU General Public License as published by 16: % the Free Software Foundation, either version 3 of the License, or 17: % (at your option) any later version. 18: % 19: % This program is distributed in the hope that it will be useful, 20: % but WITHOUT ANY WARRANTY; without even the implied warranty of 21: % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22: % GNU General Public License for more details. 23: % 24: % You should have received a copy of the GNU General Public License 25: % along with this program. If not, see <http://www.gnu.org/licenses/>. 26: 27: % $Log$ 28: function savefigs(filename,readme_text,data_str) 29: isoctave=(exist('OCTAVE_VERSION','builtin')~=0); 30: if(isoctave) 31: prints=struct('suff',{'png','eps'},... 32: 'func',{@(x)print('-dpng',x),@(x)print('-depsc2',x)}); 33: else 34: prints=struct('suff',{'png','eps','fig'},... 35: 'func',{@(x)print('-dpng',x),@(x)print('-depsc2',x), ... 36: @hgsave}); 37: end 38: n_printfuncs=length(prints); 39: for i_printfunc=1:n_printfuncs % fig,png, and eps files 40: prints(i_printfunc).func([filename,'.',prints(i_printfunc).suff]); 41: end % for i_printfunc=1:n_printfuncs 42: % document what is it 43: fid=fopen([filename,'_README','.txt'],'wt'); 44: fprintf(fid,'%s',readme_text); 45: fclose(fid); 46: % save the vector/matrix for future crunching 47: save([filename,'_data','.mat'],'data_str');
Copyright (C) 2015 by Avi Gozolchiani. See the License for information about copying.