Skip to content

Instantly share code, notes, and snippets.

@Kappie
Last active April 18, 2017 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kappie/55a335a53be8b2e55ec2253e7f281ca6 to your computer and use it in GitHub Desktop.
Save Kappie/55a335a53be8b2e55ec2253e7f281ca6 to your computer and use it in GitHub Desktop.
my_export_fig.m
function my_export_fig(filename, scriptname, save_to_thesis_plots_folder)
if nargin == 2
save_to_thesis_plots_folder = false;
end
full_path = fullfile(Constants.PLOTS_DIR, filename);
set(gcf, 'PaperPosition', [0 0 7 5.6]); %Position plot at left hand corner with width 5 and height 5.
set(gcf, 'PaperSize', [7 5.6]); %Set the paper to have width 5 and height 5.
saveas(gcf, full_path, 'pdf') %Save figure
% export_fig(full_path);
[~, name, extension] = fileparts(filename);
if save_to_thesis_plots_folder
% Also save data to file in thesis data folder
data_filename = [name '.dat'];
full_path = fullfile(Constants.THESIS_PLOTS_DATA_DIR, data_filename)
store_data_of_current_fig_to_file(full_path)
end
% Also save figure as .fig so I can edit it later.
% matlab_figure_filename = [name, '.fig'];
% matlab_figures_path = fullfile(Constants.PLOTS_DIR, 'matlab_figures', matlab_figure_filename);
% savefig(matlab_figures_path);
% Also save as tikz figure
% tikz_figure_filename = [name, '.tikz'];
% tikz_figure_path = fullfile(Constants.PLOTS_DIR, 'latex_figures', tikz_figure_filename);
% % matlab2tikz(tikz_figure_path, 'height', '\figureheight', 'width', '\figurewidth');
% matlab2tikz(tikz_figure_path);
%
% if save_to_thesis_plots_folder
% thesis_plots_path = fullfile(Constants.THESIS_PLOTS_DIR, tikz_figure_filename);
% copyfile(tikz_figure_path, thesis_plots_path);
% end
% also save a copy of the script that generated the plot,
% for reproducibility later.
script_backup_dir = fullfile(Constants.PLOTS_DIR, 'matlab_scripts');
% The name of the script is the same as the plot that it generated
script_backup_name = [name, '.m'];
script_backup_path = fullfile(script_backup_dir, script_backup_name);
script_path = fullfile(Constants.BASE_DIR, 'scripts', scriptname);
copyfile(script_path, script_backup_path);
append_date(script_backup_path)
end
function append_date(filename)
% append as matlab comment. Double percent is there so that
% fprintf writes a raw percent sign. (instead of interpreting it as some operator)
date_string = ['%% generated plot at ' datestr(now)];
file_id = fopen(filename, 'a');
fprintf(file_id, date_string)
fclose(file_id);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment