Home > FMAToolbox > Database > DBAddFigure.m

DBAddFigure

PURPOSE ^

DBAddFigure - Save figure in current database.

SYNOPSIS ^

function DBAddFigure(f,eid,name,comments,parameters,mfiles,varargin)

DESCRIPTION ^

DBAddFigure - Save figure in current database.

 Save figure as MATLAB fig and PNG image. Also save code used to generate it
 (although not required, installing <a href="http://sites.google.com/site/oliverwoodford/software/export_fig">export_fig</a> will yield better PNG images).

  USAGE

    DBAddFigure(f,eid,name,comments,parameters,mfiles,<options>)

    f              figure handle
    eid            experiment ID (identifier string)
    name           descriptive name
    comments       comment string
    parameters     figure parameters
    mfiles         cell array of m-file names (used to generate the figure)
    <options>      optional list of property-value pairs (see table below)

    =========================================================================
     Properties    Values
    -------------------------------------------------------------------------
     'onlyPNG'     skip fig and only store png capture (use this to avoid
                   storing very large figures into the database,
                   default = 'off')
    =========================================================================

  NOTE

    The pair (eid,name) must uniquely define the figure (they are used as
    primary keys in the database).

  SEE

    See also DBAddVariable, DBGetFigures, DBExportGallery.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function DBAddFigure(f,eid,name,comments,parameters,mfiles,varargin)
0002 
0003 %DBAddFigure - Save figure in current database.
0004 %
0005 % Save figure as MATLAB fig and PNG image. Also save code used to generate it
0006 % (although not required, installing <a href="http://sites.google.com/site/oliverwoodford/software/export_fig">export_fig</a> will yield better PNG images).
0007 %
0008 %  USAGE
0009 %
0010 %    DBAddFigure(f,eid,name,comments,parameters,mfiles,<options>)
0011 %
0012 %    f              figure handle
0013 %    eid            experiment ID (identifier string)
0014 %    name           descriptive name
0015 %    comments       comment string
0016 %    parameters     figure parameters
0017 %    mfiles         cell array of m-file names (used to generate the figure)
0018 %    <options>      optional list of property-value pairs (see table below)
0019 %
0020 %    =========================================================================
0021 %     Properties    Values
0022 %    -------------------------------------------------------------------------
0023 %     'onlyPNG'     skip fig and only store png capture (use this to avoid
0024 %                   storing very large figures into the database,
0025 %                   default = 'off')
0026 %    =========================================================================
0027 %
0028 %  NOTE
0029 %
0030 %    The pair (eid,name) must uniquely define the figure (they are used as
0031 %    primary keys in the database).
0032 %
0033 %  SEE
0034 %
0035 %    See also DBAddVariable, DBGetFigures, DBExportGallery.
0036 %
0037 
0038 % Copyright (C) 2007-2018 by Michaƫl Zugaro
0039 %
0040 % This program is free software; you can redistribute it and/or modify
0041 % it under the terms of the GNU General Public License as published by
0042 % the Free Software Foundation; either version 3 of the License, or
0043 % (at your option) any later version.
0044 
0045 global dbUser;
0046 
0047 % Make sure MyM is installed and functional
0048 CheckMyM;
0049 
0050 % Check number of parameters
0051 if nargin < 6,
0052     error('Incorrect number of parameters (type ''help <a href="matlab:help DBAddFigure">DBAddFigure</a>'' for details).');
0053 end
0054 
0055 % Default values
0056 onlyPNG = 'off';
0057 
0058 % Check parameters
0059 if ~ishandle(f),
0060     error('Incorrect figure handle (type ''help <a href="matlab:help DBAddFigure">DBAddFigure</a>'' for details).');
0061 end
0062 if ~ischar(eid),
0063     error('Incorrect EID string (type ''help <a href="matlab:help DBAddFigure">DBAddFigure</a>'' for details).');
0064 end
0065 if ~ischar(name),
0066     error('Incorrect figure name (type ''help <a href="matlab:help DBAddFigure">DBAddFigure</a>'' for details).');
0067 end
0068 if ~isempty(strfind(eid,'/')) || ~isempty(strfind(name,'/')),
0069     error('EIDs and names should not contain ''/'' (type ''help <a href="matlab:help DBAddFigure">DBAddFigure</a>'' for details).');
0070 end
0071 if ~ischar(comments),
0072     error('Incorrect comment string (type ''help <a href="matlab:help DBAddFigure">DBAddFigure</a>'' for details).');
0073 end
0074 if ~ischar(parameters),
0075     error('Incorrect figure parameters (type ''help <a href="matlab:help DBAddFigure">DBAddFigure</a>'' for details).');
0076 end
0077 if ~ischar(mfiles) && ~iscell(mfiles),
0078     error('Incorrect list of m-files (type ''help <a href="matlab:help DBAddFigure">DBAddFigure</a>'' for details).');
0079 end
0080 if ischar(mfiles),
0081     mfiles = {mfiles};
0082 end
0083 
0084 % Parse options
0085 for i = 1:2:length(varargin),
0086     if ~ischar(varargin{i}),
0087         error(['Parameter ' num2str(i+firstIndex) ' is not a property (type ''help <a href="matlab:help DBAddFigure">DBAddFigure</a>'' for details).']);
0088     end
0089     switch(lower(varargin{i})),
0090         case 'onlypng',
0091             onlyPNG = lower(varargin{i+1});
0092             if ~isastring(onlyPNG,'on','off'),
0093                 error('Incorrect value for property ''onlypng'' (type ''help <a href="matlab:help DBAddFigure">DBAddFigure</a>'' for details).');
0094             end
0095         otherwise,
0096             error(['Unknown property ''' num2str(varargin{i}) ''' (type ''help <a href="matlab:help DBAddFigure">DBAddFigure</a>'' for details).']);
0097     end
0098 end
0099 
0100 onlyPNG = strcmp(onlyPNG,'on');
0101 
0102 basename = tempname;
0103 figName = [basename '.fig'];
0104 pngName = [basename '.png'];
0105 if ~onlyPNG,
0106     try
0107         % Create temporary fig file
0108         saveas(f,figName);
0109     catch
0110         error('Could not create temporary file for fig figure.');
0111     end
0112 end
0113 try
0114     % Create temporary png file
0115     if ~isempty(which('export_fig')),
0116         export_fig(f,pngName,'-png','-a1');
0117     else
0118         saveas(f,pngName);
0119     end
0120 catch
0121     error('Could not create temporary file for png figure.');
0122 end
0123 
0124 % M-Files
0125 code{1} = '';
0126 for i = 1:length(mfiles),
0127     mfileName = which(mfiles{i});
0128     if isempty(mfileName),
0129         error(['M-File for function ''' mfiles{i} ''' not found.']);
0130     end
0131     fid = fopen(mfileName);
0132     code{i} = fread(fid);
0133     fclose(fid);
0134 end
0135 
0136 % Date and md5
0137 d = datestr(now);
0138 md5 = CalcMD5(pngName);
0139 
0140 % Execute SQL query command
0141 if onlyPNG,
0142     h = mym(['insert into figures (eid,name,comments,parameters,mfiles,code,date,user,md5,png) values ("{S}","{S}","{S}","{S}","{M}","{M}","{S}","{S}","{Si}","{uF}")'],eid,name,comments,parameters,mfiles,code,d,dbUser,md5,pngName);
0143 else
0144     h = mym(['insert into figures (eid,name,comments,parameters,mfiles,code,date,user,md5,fig,png) values ("{S}","{S}","{S}","{S}","{M}","{M}","{S}","{S}","{Si}","{F}","{uF}")'],eid,name,comments,parameters,mfiles,code,d,dbUser,md5,figName,pngName);
0145 end
0146 
0147 % Delete temporary files
0148 if ~onlyPNG,
0149     delete(figName);
0150 end
0151 delete(pngName);

Generated on Fri 16-Mar-2018 13:00:20 by m2html © 2005