Home > database > mysql_stimfld2attrib.m

mysql_stimfld2attrib

PURPOSE ^

Associates stimuli with an attribute

SYNOPSIS ^

function attrib_id = mysql_stimfld2attrib(varargin)

DESCRIPTION ^

 Associates stimuli with an attribute

 attrib_id = mysql_stimfld2attrib(varargin)

 Input arguments are tag/value pairs

 Tags:
 'attribute' - name of attribute to be matched in attribute table
 'stimtbl_fld' - stimulus table field to match values in
 'stimtbl_vals' - values to search for in stimulus table field
 'conn_id' - mysql connection ID

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function attrib_id = mysql_stimfld2attrib(varargin)
0002 % Associates stimuli with an attribute
0003 %
0004 % attrib_id = mysql_stimfld2attrib(varargin)
0005 %
0006 % Input arguments are tag/value pairs
0007 %
0008 % Tags:
0009 % 'attribute' - name of attribute to be matched in attribute table
0010 % 'stimtbl_fld' - stimulus table field to match values in
0011 % 'stimtbl_vals' - values to search for in stimulus table field
0012 % 'conn_id' - mysql connection ID
0013 
0014 % 03/31/09 PJ
0015 % 07/18/09 PJ - extracted portion of code into mysql_check_attribute
0016 % 21Nov2013 PJ - added checking and conversion of string to cell during fldval handling
0017 
0018 % Parse the input arguments
0019 narg = length(varargin);
0020 for iarg = 1:2:narg
0021   switch varargin{iarg}
0022     case 'conn_id'
0023       conn_id = varargin{iarg+1};
0024     case {'attrib','attribute'}
0025       attrib_tag = varargin{iarg+1};
0026     case 'stimtbl_fld'
0027       fldname = varargin{iarg+1};
0028     case 'stimtbl_vals'
0029       fldvals = varargin{iarg+1};
0030     otherwise
0031       fprintf('%s: Unknown input argument: %s\n', mfilename, varargin{iarg});
0032   end
0033 end
0034 
0035 % Check for valid connection to database
0036 if ~exist('conn_id','var') || isempty(conn_id) || mysql(conn_id,'status')
0037   error('%s: Do not have a valid connection ID', mfilename);
0038 end
0039 
0040 params.conn_id = conn_id;
0041 params.create = true;
0042 attrib_id = mysql_check_attribute(attrib_tag, params);
0043 
0044 %
0045 % Get the list of stimulus IDs that correspond to the current stimulus set
0046 %
0047 switch fldname
0048   case {'name','description','playlist','artist','album','genre','location'}
0049     if ~iscell(fldvals)
0050       fldvals = {fldvals};
0051     end
0052     stim_str = sprintf('"%s"',cell2str(fldvals,'","'));
0053   otherwise
0054     stim_str = sprintf('%d,', fldvals);
0055     stim_str(end) = '';
0056 end
0057 mysql_str = sprintf('SELECT stimulus_id FROM stimulus WHERE %s IN (%s);', fldname, stim_str);
0058 stim_ids = mysql(conn_id,mysql_str);
0059 
0060 %
0061 % Get a list of stimulus IDs that are already associated with the attribute
0062 %
0063 
0064 mysql_str = sprintf(['SELECT stimulus_id FROM stimulus_x_attribute ' ...
0065     'WHERE attribute_id=%d'], attrib_id);
0066 existing_stim_ids = mysql(conn_id, mysql_str);
0067 
0068 % Determine which stim_ids to insert
0069 insert_stim_ids = setdiff(stim_ids, existing_stim_ids);
0070 insert_stim_ids = insert_stim_ids(:);
0071 
0072 % Insert the new stim IDs
0073 if ~isempty(insert_stim_ids)
0074   value_str = sprintf('("%d","%d"),', ...
0075       [insert_stim_ids ones(size(insert_stim_ids))*attrib_id]');
0076   value_str(end) = [];
0077   mysql_str = sprintf(['INSERT INTO stimulus_x_attribute (stimulus_id,' ...
0078     ' attribute_id) VALUES %s;'], value_str);
0079   mysql(conn_id, mysql_str);
0080 else
0081   fprintf('No stimulus IDs to be inserted\n');
0082 end
0083 
0084 return

Generated on Sun 25-Aug-2019 04:00:39 by m2html © 2003