function closejob(base,figids,figext,recdir,figdir)
% CLOSEJOB Saves protocol and figures of the job
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The function is good for finishing a run that generated diary and
% figures. It fulfiles several tasks in dependence on input parameters:
% 1. It stops diarying.
% 2. The diary file is copied into a default or a user-defined directory.
% 3. Selected figures are stored in a default or a user-defined directory
% in required formats.
% An application of the function is convenient namely for finishing jobs
% with experimental data processing, when records and figures have to be
% stored for later use in reports or as a part of documentation.
% Calls:
% closejob(base)
% closejob(base,figids)
% closejob(base,figids,figext)
% closejob(base,figids,figext,recdir)
% closejob(base,figids,figext,recdir,figdir)
% Input parameters:
% base user defined base of file names of records and figures
% figext cell array of figure files extensions (default {'fig'})
% figids cell array of figure identifiers (handles, default {gcf})
% recdir name of directory for storing records (default 'Records')
% figdir name of directory for storing figures (default 'Figures')
% Default directories are generated in the working directory if no
% present there.
% Prerequisities:
% inp.m (FEX Id. 9033) Keyboard input with default value
% freename.m (FEX Id. 9036) Find free file name in directory
% separator (FEX Id. 11725) Make smart separating line with a text
% savefig (FEX Id. 10889) Save figures in selected formats.
%
% See also: openjob
%
% Examples:
% closejob('meas') % Store record under the name 'meas-vw.txt in the
% subdirectory 'Records', and the last figure in a file named
% meas-vw-xy.fig in the subdirectory 'Figures'. If the sudirectories
% do not exist, create them.
% Characters v, w, x, y represent digits, where
% vw is the first free order of filename with the same base and
% xy belong to the figure number (handle).
% closejob('meas',{}) % Store record without figures.
% closejob('data',fids,figext) % Common part of file names will be
% 'data-vw'. Only figures defined in fids will be stored. The form
% of fids is {gcf1, gcf2, ...}. Each figure is stored in formats
% required in definition of figext, say {'fig','png','pdf','eps'}
% closejob('data',fids,{'eps','pdf'},'') % As before, but without storing
% a record.
% closejob('data',fids,figext,'.','.') % Store all in working directory
% closejob('data',fids,figext,'Archive','Archive') % Store both record
% and figures in the same subdirectoty 'Archive')
% Miroslav Balda
% miroslav AT balda DOT cz
% 2006-03-25 v 1.0 first simple version with the name savejob.m
% 2008-08-14 v 1.1 complete reconstruction of v 1.0 to closejob.m
% 2008-11-24 v 1.2 saving of figures implemented by function savefig.m
% 2009-01-12 v 1.3 improved code in creating names of stored figures
% 2009-02-10 v 1.4 removed bug in saving a non-existing figure
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
diary off
if nargin<1 || ~strcmp(inp('save job','yes'),'yes')
if exist('diary','file'), delete diary, end
return
end
% TEST PREREQUISITIES
Id = '';
if ~exist('inp.m','file'), Id=[Id 'inp (FEX Id=9033) ']; end
if ~exist('freename.m','file'), Id=[Id 'freename (FEX Id=9036) ']; end
if ~exist('separator.m','file'), Id=[Id 'separator(FEX Id=11725) ']; end
if ~exist('savefig.m','file'), Id=[Id 'savefig(FEX Id=10889) ']; end
if ~isempty(Id)
error(['Download function(s) ' Id 'from File Exchange'])
end
if nargin<5
figdir = 'Figures'; % subdirectory for saving figures
if nargin<4
recdir = 'Records'; % subdirectory for saving records
if nargin<3
figext = {'fig'}; % default extension
if nargin<2
if isempty(get(0,'CurrentFigure'))
figids = {};
else
figids = {gcf}; % current figure and axes
end
end
end
end
end
if ~iscell(figids)
error('The second argument should be a cell array')
end
if ~iscell(figext)
error('The third argument should be a cell array')
end
% MAKE DIRECTORIES if do not exist
if ~isempty(recdir) && ~exist(['.\' recdir],'dir')
mkdir(recdir); % for saving diary
end
if ~isempty(figdir) && ~exist(['.\' figdir],'dir') && ~isempty(figids)
mkdir(figdir); % for saving figures
end
% CREATE NAMES OF FILES
saverf = 1;
if ~isempty(recdir)
name = freename(recdir,[base '-'],2); % if exist recdir
else
if ~isempty(figdir)
name = freename(figdir,[base '-'],2);% if recdir doesn't exist
else
saverf = 0;
end
end
if saverf==0
delete diary
return
end
diary on
separator([name '.txt']); % display record name
diary off
fprintf(' WAIT\n');
% SAVING OF RECORDS
if ~isempty(recdir)
copyfile('diary',[recdir '\' name '.txt']); % protocol
end
delete diary
% SAVING OF FIGURES
isfig = ismember(figext,'fig');
figext(isfig) = []; % clear extension 'fig' from figext
isfig = any(isfig);
nfigs = length(figids);
% save figures in all formats
for j = 1:nfigs % Cycle of figures
gcfj = figids{j};
fname = [figdir '\' name];
if nfigs>1 % complement figure #
fname =[fname '-' sprintf('%02d',gcfj)];
end
if isfig
saveas(gcfj,fname); % save *.fig
end
if ~isempty(figext) % save all other formats
savefig(fname,gcfj,figext{:});
end
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
end
fprintf(' FINISHED\n\n');