Home > stim_scripts > two_afc_select_basic.m

two_afc_select_basic

PURPOSE ^

Selects trials for a Two-Alternative Forced Choice task

SYNOPSIS ^

function [trial_id] = two_afc_select_basic(varargin)

DESCRIPTION ^

  Selects trials for a Two-Alternative Forced Choice task

 [trial_id] = two_afc_select_basic(varargin);

 Copyright (c) 2006-13 The Regents of the University of California, Davis campus. All Rights Reserved.

 Author: Petr Janata
 Edits by JR
 Questions? contact jrector@ucdavis.edu

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [trial_id] = two_afc_select_basic(varargin)
0002 %  Selects trials for a Two-Alternative Forced Choice task
0003 %
0004 % [trial_id] = two_afc_select_basic(varargin);
0005 %
0006 % Copyright (c) 2006-13 The Regents of the University of California, Davis campus. All Rights Reserved.
0007 %
0008 % Author: Petr Janata
0009 % Edits by JR
0010 % Questions? contact jrector@ucdavis.edu
0011 
0012 script_version = 'two_afc_select_basic_v1.0';
0013 
0014 trial_id = '';
0015 
0016 % Parse input parameters
0017 for iarg = 1:2:nargin
0018   curr_arg = varargin{iarg};
0019   
0020   switch curr_arg
0021       case {'params','params_file','param_file'}
0022           param_file = varargin{iarg+1};    
0023   end % swich curr_arg
0024 end % for iarg
0025 
0026 % Make sure we have necessary variables
0027 if ~exist('param_file') || isempty(param_file)
0028   fprintf('No parameter file specified\n');
0029   return
0030 end
0031 
0032 persistent is_initialized 
0033 persistent master_trial_list
0034 
0035 %
0036 % Handle initialization
0037 %
0038 if isempty(is_initialized)  
0039   master_trial_list = [];
0040   
0041   %Initialize random number generator
0042   rand('state', sum(100*clock));
0043   
0044   % Get parameters from a parameters file that is passed in as an input argument
0045   params_fh = str2func(param_file);
0046   params = params_fh();
0047 
0048   % determine experiment name for logging
0049   if (isfield(params,'ensemble') && isfield(params.ensemble,'expname') && ~isempty(params.ensemble.expname))
0050     expname = params.ensemble.expname;
0051   else
0052     expname = 'unknown';
0053   end
0054 
0055   % Establish a connection to the database
0056   conn_id = params.mysql.conn_id;
0057   if mysql_check_conn(conn_id)
0058     mysql_make_conn(params.mysql);
0059   end
0060 
0061   %
0062   % Identify which trials are part of this experiment using the
0063   % trial_x_attribute table
0064   %
0065   
0066   % Retrieve the attribute ID
0067   mysql_str = sprintf(['SELECT attribute_id FROM attribute ' ...
0068     'WHERE name="%s";'], params.attrib_name);
0069   attrib_id = mysql(conn_id, mysql_str);
0070   
0071   % Get the list of trials
0072   mysql_str = sprintf(['SELECT trial_id FROM trial_x_attribute ' ...
0073     'WHERE attribute_id=%d'], attrib_id);
0074   trial_ids = mysql(conn_id, mysql_str);
0075   
0076   trial_id_str = sprintf('"%d",', trial_ids);
0077   trial_id_str(end) = '';
0078   
0079   % Load the trial information
0080   varlist = {'trial_id','data_format_id','correct_response_enum','stimulus_id1','stimulus_id2'};
0081   numvars = length(varlist);
0082   cols = set_var_col_const(varlist);
0083   
0084   mysql_str = sprintf(['SELECT %s FROM trial WHERE trial_id IN (%s);'], ...
0085       cell2str(varlist,','), trial_id_str);
0086   [td{1:numvars}] = mysql(conn_id, mysql_str);
0087   
0088   % Check to see if we are dealing with homogeneous trial types
0089   dfid = unique(td{cols.data_format_id});
0090   if length(dfid) > 1
0091     fprintf('Too many data format IDs associated with trials\n');
0092     return
0093   end  
0094   
0095   % Add all available trials to the list if param is set accordingly
0096   if params.include_all_trials
0097     master_trial_list = td{cols.trial_id};
0098   else
0099     % implement logic here to determine which trials to include
0100   end
0101 
0102   % Shuffle the master trial list
0103   num_trials = length(master_trial_list);
0104   master_trial_list = master_trial_list(randperm(num_trials));
0105 
0106   fprintf('%s for experiment ''%s'': Initialized %d trials\n', mfilename, expname, num_trials);
0107   is_initialized = true;
0108 end % if isempty(is_initialized)
0109 
0110 % Select the first trial ID from the list and eliminate it from the list
0111 if ~isempty(master_trial_list)
0112   trial_id = master_trial_list(1);
0113   master_trial_list(1) = [];
0114   
0115   trial_id = num2str(trial_id);
0116 else
0117   trial_id = 'end';
0118 end

Generated on Sat 24-Aug-2019 04:00:39 by m2html © 2003