Home > FMAToolbox > Database > DBGetVariables.m

DBGetVariables

PURPOSE ^

DBGetVariables - Get all variables that match given criteria.

SYNOPSIS ^

function x = DBGetVariables(query,varargin)

DESCRIPTION ^

DBGetVariables - Get all variables that match given criteria.

 Get variables and related information such as code used to generate them.

 This is mostly useful to get information about the variables (who stored
 them, when, which code was used, with what parameters, etc.) However, if
 you are only interested in the actual values for subsequent processing
 - which is the most frequent scenario -, it will be more appropriate to
 use <a href="DBGetValues">DBGetValues</a> instead, which provides a more useful output: the values
 will be arranged into a matrix or 3D array, rather than a cumbersome
 structure of cell arrays.

 There are exceptions, however. Some types of data, such as character strings
 of varying lengths or matrices of unequal sizes, cannot be grouped into a
 matrix or 3D array. In such cases, you will not be able to use <a href="DBGetValues">DBGetValues</a>,
 and will have to use DBGetVariables instead.

  USAGE

    x = DBGetVariables(query,<options>)

    query          optional database query (WHERE clause; see Example)
    <options>      optional list of property-value pairs (see table below)

    =========================================================================
     Properties    Values
    -------------------------------------------------------------------------
     'output'       'variables' to get the variables but not the information,
                    i.e. eid, name, comments, parameters, etc. (default),
                    'info' for the information but not the variables, 'full'
                    for both, or 'keys' for eid and name only
    =========================================================================

  OUTPUT

    x is a structure with the following fields:

    eid            experiment ID (identifier string)
    name           figure descriptive name (identifier string)
    v              variable value
    comments       comments
    parameters     figure parameters
    code           text of m-files used to generate the figure
    date           date when the figure was saved
    user           the user that saved the figure

    Each field is a cell array.

  EXAMPLE

    Get all variables from experiment "experiment1", the name of which starts
    with "raster" (for details, see an SQL manual):

    x = DBGetVariables('eid="experiment1" and name like "raster%"');

  SEE

    See also DBGetValues, DBAddVariable, DBGetFigures, DBDisplay.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function x = DBGetVariables(query,varargin)
0002 
0003 %DBGetVariables - Get all variables that match given criteria.
0004 %
0005 % Get variables and related information such as code used to generate them.
0006 %
0007 % This is mostly useful to get information about the variables (who stored
0008 % them, when, which code was used, with what parameters, etc.) However, if
0009 % you are only interested in the actual values for subsequent processing
0010 % - which is the most frequent scenario -, it will be more appropriate to
0011 % use <a href="DBGetValues">DBGetValues</a> instead, which provides a more useful output: the values
0012 % will be arranged into a matrix or 3D array, rather than a cumbersome
0013 % structure of cell arrays.
0014 %
0015 % There are exceptions, however. Some types of data, such as character strings
0016 % of varying lengths or matrices of unequal sizes, cannot be grouped into a
0017 % matrix or 3D array. In such cases, you will not be able to use <a href="DBGetValues">DBGetValues</a>,
0018 % and will have to use DBGetVariables instead.
0019 %
0020 %  USAGE
0021 %
0022 %    x = DBGetVariables(query,<options>)
0023 %
0024 %    query          optional database query (WHERE clause; see Example)
0025 %    <options>      optional list of property-value pairs (see table below)
0026 %
0027 %    =========================================================================
0028 %     Properties    Values
0029 %    -------------------------------------------------------------------------
0030 %     'output'       'variables' to get the variables but not the information,
0031 %                    i.e. eid, name, comments, parameters, etc. (default),
0032 %                    'info' for the information but not the variables, 'full'
0033 %                    for both, or 'keys' for eid and name only
0034 %    =========================================================================
0035 %
0036 %  OUTPUT
0037 %
0038 %    x is a structure with the following fields:
0039 %
0040 %    eid            experiment ID (identifier string)
0041 %    name           figure descriptive name (identifier string)
0042 %    v              variable value
0043 %    comments       comments
0044 %    parameters     figure parameters
0045 %    code           text of m-files used to generate the figure
0046 %    date           date when the figure was saved
0047 %    user           the user that saved the figure
0048 %
0049 %    Each field is a cell array.
0050 %
0051 %  EXAMPLE
0052 %
0053 %    Get all variables from experiment "experiment1", the name of which starts
0054 %    with "raster" (for details, see an SQL manual):
0055 %
0056 %    x = DBGetVariables('eid="experiment1" and name like "raster%"');
0057 %
0058 %  SEE
0059 %
0060 %    See also DBGetValues, DBAddVariable, DBGetFigures, DBDisplay.
0061 %
0062 
0063 % Copyright (C) 2007-2016 by Michaƫl Zugaro
0064 %
0065 % This program is free software; you can redistribute it and/or modify
0066 % it under the terms of the GNU General Public License as published by
0067 % the Free Software Foundation; either version 3 of the License, or
0068 % (at your option) any later version.
0069 
0070 % Make sure MyM is installed and functional
0071 CheckMyM;
0072 
0073 % Default values
0074 output = 'variables';
0075 
0076 % Optional query provided?
0077 if nargin == 0,
0078     query = '';
0079 elseif isastring(query,'output'),
0080     varargin = {query varargin{:}};
0081     query = '';
0082 end
0083 
0084 % Edit query
0085 query = strtrim(query);
0086 query = regexprep(query,'^where','');
0087 if ~isempty(query), query = [' where ' query]; end
0088 
0089 % Parse options
0090 for i = 1:2:length(varargin),
0091     if ~ischar(varargin{i}),
0092         error(['Parameter ' num2str(i+1) ' is not a property (type ''help <a href="matlab:help DBGetVariables">DBGetVariables</a>'' for details).']);
0093     end
0094     switch(lower(varargin{i})),
0095         case 'output',
0096             output = lower(varargin{i+1});
0097             if ~isastring(output,'variables','info','full','keys'),
0098                 error('Incorrect value for property ''output'' (type ''help <a href="matlab:help DBGetVariables">DBGetVariables</a>'' for details).');
0099             end
0100         otherwise,
0101             error(['Unknown property ''' num2str(varargin{i}) ''' (type ''help <a href="matlab:help DBGetVariables">DBGetVariables</a>'' for details).']);
0102     end
0103 end
0104 
0105 % Query database
0106 switch output,
0107     case 'full',
0108         x = mym(['select v,eid,name,comments,parameters,mfiles,code,date,user from variables' query]);
0109     case 'variables',
0110         % Although final output does not require (eid,name), we retrieve them for potential error messages (and discard them later)
0111         x = mym(['select v,eid,name from variables' query]);
0112     case 'info',
0113         x = mym(['select eid,name,comments,parameters,mfiles,code,date,user from variables' query]);
0114     case 'keys',
0115         x = mym(['select eid,name from variables' query]);
0116 end
0117 
0118 % Make sure query results are not empty
0119 if isempty(x),
0120     warning(['No variables match (' query ').']);
0121 end
0122 
0123 % Load variables from external storage if necessary
0124 if isfield(x,'v'),
0125     for i = 1:length(x.v),
0126         matFile = x.v{i};
0127         if isastring(matFile) && length(matFile) > 4 && matFile(1) == '/' && strcmp('.mat',matFile(end-3:end)),
0128             if ~exist(matFile,'file'),
0129                 error(['External storage file for (' x.eid{i} ',' x.name{i} ') is missing.']); 
0130             else
0131                 a = load(matFile);
0132                 x.v{i} = a.v;
0133             end
0134         end
0135     end
0136 end
0137 
0138 % These extra fields can now be removed (see note above)
0139 if strcmp(output,'variables'),
0140     x = rmfield(x,{'eid','name'});
0141 end
0142 
0143 % Format code
0144 if strcmp(output,'full') || strcmp(output,'info'),
0145     code = {};
0146     for i = 1:length(x.code),
0147         for j = 1:length(x.code{i}),
0148             code{i}{j} = char(x.code{i}{j})';
0149         end
0150     end
0151     x.code = code;
0152 end

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