0001 function [trial_id] = two_afc_select_basic(varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 script_version = 'two_afc_select_basic_v1.0';
0013
0014 trial_id = '';
0015
0016
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
0024 end
0025
0026
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
0037
0038 if isempty(is_initialized)
0039 master_trial_list = [];
0040
0041
0042 rand('state', sum(100*clock));
0043
0044
0045 params_fh = str2func(param_file);
0046 params = params_fh();
0047
0048
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
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
0063
0064
0065
0066
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
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
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
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
0096 if params.include_all_trials
0097 master_trial_list = td{cols.trial_id};
0098 else
0099
0100 end
0101
0102
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
0109
0110
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