Home > FMAToolbox > Database > DBAddVariable.m

DBAddVariable

PURPOSE ^

DBAddVariable - Save variable in current database.

SYNOPSIS ^

function DBAddVariable(v,eid,name,parameters,comments,mfiles)

DESCRIPTION ^

DBAddVariable - Save variable in current database.

 Save variable and code used to compute it.

  USAGE

    DBAddVariable(v,eid,name,parameters,comments,mfiles)

    v              variable
    eid            experiment ID (identifier string)
    name           descriptive name
    comments       comment string
    parameters     parameters
    mfiles         cell array of m-file names (used to compute the variable)

  NOTE

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

  CUSTOM DEFAULTS

    It is possible to set the maximum size stored within the database vs
    in external files. See <a href="matlab:help Database">Database</a>'' for details.

  SEE

    See also DBAddFigure, DBGetVariables, DBGroupValues.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function DBAddVariable(v,eid,name,parameters,comments,mfiles)
0002 
0003 %DBAddVariable - Save variable in current database.
0004 %
0005 % Save variable and code used to compute it.
0006 %
0007 %  USAGE
0008 %
0009 %    DBAddVariable(v,eid,name,parameters,comments,mfiles)
0010 %
0011 %    v              variable
0012 %    eid            experiment ID (identifier string)
0013 %    name           descriptive name
0014 %    comments       comment string
0015 %    parameters     parameters
0016 %    mfiles         cell array of m-file names (used to compute the variable)
0017 %
0018 %  NOTE
0019 %
0020 %    The pair (eid,name) must uniquely define the variable (they are used as
0021 %    primary keys in the database).
0022 %
0023 %  CUSTOM DEFAULTS
0024 %
0025 %    It is possible to set the maximum size stored within the database vs
0026 %    in external files. See <a href="matlab:help Database">Database</a>'' for details.
0027 %
0028 %  SEE
0029 %
0030 %    See also DBAddFigure, DBGetVariables, DBGroupValues.
0031 %
0032 
0033 % Copyright (C) 2007-2016 by Michaƫl Zugaro
0034 %
0035 % This program is free software; you can redistribute it and/or modify
0036 % it under the terms of the GNU General Public License as published by
0037 % the Free Software Foundation; either version 3 of the License, or
0038 % (at your option) any later version.
0039 
0040 global dbUser;
0041 defaultMaxSize = 2^16-1;
0042 
0043 % Make sure MyM is installed and functional
0044 CheckMyM;
0045 
0046 % Check number of parameters
0047 if nargin < 6,
0048     error('Incorrect number of parameters (type ''help <a href="matlab:help DBAddVariable">DBAddVariable</a>'' for details).');
0049 end
0050 
0051 % Check parameters
0052 if ~ischar(eid),
0053     error('Incorrect EID string (type ''help <a href="matlab:help DBAddVariable">DBAddVariable</a>'' for details).');
0054 end
0055 if ~ischar(name),
0056     error('Incorrect figure name (type ''help <a href="matlab:help DBAddVariable">DBAddVariable</a>'' for details).');
0057 end
0058 if ~isempty(strfind(eid,'/')) || ~isempty(strfind(name,'/')),
0059         error('EIDs and names should not contain ''/'' (type ''help <a href="matlab:help DBAddVariable">DBAddVariable</a>'' for details).');
0060 end
0061 if ~ischar(comments),
0062     error('Incorrect comment string (type ''help <a href="matlab:help DBAddVariable">DBAddVariable</a>'' for details).');
0063 end
0064 if isempty(comments),
0065     comments = '-';
0066 end
0067 if ~ischar(parameters),
0068     error('Incorrect figure parameters (type ''help <a href="matlab:help DBAddVariable">DBAddVariable</a>'' for details).');
0069 end
0070 if ~ischar(mfiles) && ~iscell(mfiles),
0071     error('Incorrect list of m-files (type ''help <a href="matlab:help DBAddVariable">DBAddVariable</a>'' for details).');
0072 end
0073 if ischar(mfiles),
0074     mfiles = {mfiles};
0075 end
0076 
0077 % M-Files
0078 code = '';
0079 for i = 1:length(mfiles),
0080     mfileName = which(mfiles{i});
0081     if isempty(mfileName),
0082         error(['M-File for function ''' mfiles{i} ''' not found.']);
0083     end
0084     fid = fopen(mfileName);
0085     code{i} = fread(fid);
0086     fclose(fid);
0087 end
0088 
0089 % Date and md5
0090 d = datestr(now);
0091 try
0092     md5 = CalcMD5(v);
0093 catch
0094     warning('Could not compute MD5 (works only for numeric arrays)');
0095     md5 = 0;
0096 end
0097 
0098 % Check variable size: if too large (>64KiB by default), save to file and store path in database
0099 warning('off','FMAToolbox:GetCustomDefaults:UsingCustomDefaults');
0100 maxSize = GetCustomDefaults([],'maxsize',defaultMaxSize);
0101 warning('on','FMAToolbox:GetCustomDefaults:UsingCustomDefaults');
0102 storage = DBExternalStoragePath;
0103 s = whos('v');
0104 if s.bytes > maxSize,
0105     % store outside DB
0106     targetDirectory = [storage '/' DBUse];
0107     if ~isdir(targetDirectory), mkdir(targetDirectory); end
0108     if ~isdir([targetDirectory '/variables']), mkdir([targetDirectory '/variables']); end
0109     matFile = [targetDirectory '/variables/' eid '-' name '.mat'];
0110     h = mym(['insert into variables (eid,name,comments,parameters,mfiles,code,date,user,md5,v) values ("{S}","{S}","{S}","{S}","{M}","{M}","{S}","{S}","{Si}","{M}")'],eid,name,comments,parameters,mfiles,code,d,dbUser,md5,matFile);
0111     save(matFile,'v');
0112 else
0113     % store in DB
0114     h = mym(['insert into variables (eid,name,comments,parameters,mfiles,code,date,user,md5,v) values ("{S}","{S}","{S}","{S}","{M}","{M}","{S}","{S}","{Si}","{M}")'],eid,name,comments,parameters,mfiles,code,d,dbUser,md5,v);
0115 end

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