⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 behavior3.m

📁 Implementation to linear, quadratic and logistic discriminant analysis, for examples
💻 M
字号:
function varargout = behavior3(varargin)

% behavior3( FILENAME, METHOD, HANDEDNESS, NUM_TRIALS);
%
% Takes the following arguments:   
% 
% Required:
%   FILENAME - Name of file to store the results from this experiment in:  
%   e.g. 'blah' will store the variables choices and rewards into blah.mat
%
%   METHOD - Must be either 1, 2, or 3.  Will specify the form of the reward
%   schedule presented under this trial
% 
%   HANDEDNESS - Must be either 'l' or 'r', corresponding to left or
%   right-handed.  Specifies the dominant hand of the subject, and dictates
%   whether the keys "x" and "c" are used (left-handed), or the keys "," and
%   "." are used (right-handed)
%
% Optional:
%   NUM_TRIALS - Specifies the number of trials to run.  If left unspecified,
%   the default is 20.
%
% Examples:
%   Typically, you will ask the subject if they are left or right-handed.  If
%   they are right-handed, you might run the following experiments
% 
% >> behavior3('subject1_exp1_method1', 1, 'r', 240);
% >> behavior3('subject1_exp2_method2', 2, 'r', 240);
% >> behavior3('subject1_exp3_method3', 3, 'r', 240);
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @behavior3_OpeningFcn, ...
                   'gui_OutputFcn',  @behavior3_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before behavior3 is made visible.
function behavior3_OpeningFcn(hObject, eventdata, handles, varargin)

global CHOICES; global REWARDS; global ALLOCATIONS;
global A; global B; global NUM_TRIALS;
global key_A; global key_B; global METHOD;
CHOICES = [];
REWARDS = [];
ALLOCATIONS = 20;

% Choose default command line output for behavior3
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

if nargin < 4, 
    disp('You need to specify a filename and method and dominant hand');
    error('Unspecified filename'); 
elseif nargin < 5,
    disp('You need to specify a method and dominant hand');
    error('Unspecified method'); 
elseif nargin < 6,
    disp('You need to specify a dominant hand');
    error('Unspecified handedness'); 
else
    filename = varargin{1};    
    METHOD = varargin{2};
    hand = varargin{3};
end;

if METHOD ~= 1:3,
    error('Wrong method type');
end;
    
if hand == 'l',
    key_A = 'x';    key_B = 'c';
elseif hand =='r',
    key_A = ',';    key_B = '.';
else 
    disp('dominant hand must be either l or r');
    error('Wrong handedness');
end
if nargin>6,    NUM_TRIALS = varargin{4};    else,    NUM_TRIALS = 20;   end;

set(hObject, 'keypressfcn', {@keystart, handles, filename});

set(handles.text1, 'string', ['Press keys "' upper(key_A) '" and "' upper(key_B) '" to maximize reward.  Press Y when ready']); set(handles.axes1, 'xtick', [], 'ytick', [0 1], 'yticklabel', ['Min';'Max'], 'fontsize', 14);
set(handles.axes2, 'xtick', [], 'ytick', linspace(0,1,10), 'yticklabel', 0:9, 'fontsize', 14, 'box', 'on');

% --- Outputs from this function are returned to the command line.
function varargout = behavior3_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;

function keycallback(hObject, eventdata, handles, filename)
global CHOICES; global REWARDS;
global key_A;  global key_B; global NUM_TRIALS;

d = get(hObject, 'currentcharacter');
if d==key_A,
    CHOICES = [1 CHOICES];
elseif d==key_B,
    CHOICES = [0 CHOICES];
else,
    set(handles.text1, 'string', ['Incorrect key press, use ' upper(key_A) ' and ' upper(key_B) '.']); 
    return;
end;
set(handles.text1, 'string', ['Reward computing']); 
pause(0.25);
reward = reward_func;
REWARDS = [reward REWARDS];
if length(CHOICES)<NUM_TRIALS,
    axes(handles.axes1);
    bar(0.5, REWARDS(1), 0.2, 'r');
    set(handles.axes1, 'xtick', [], 'ytick', [0 1], 'yticklabel', ['Min';'Max'], 'fontsize', 14);
    xlim([0 1]); ylim([0 1]);
    set(handles.text1, 'string', [int2str(length(CHOICES)) ' Trials Completed']); 
    axes(handles.axes2);
    bar(0.5, sum(REWARDS), 0.2, 'b');    
    set(handles.axes2, 'xtick', [], 'ytick', linspace(0, NUM_TRIALS, 10), 'yticklabel', 0:9, 'fontsize', 14, 'box', 'on');
    xlim([0 1]); ylim([0 NUM_TRIALS]);
    
    if (length(find((sum(REWARDS)-linspace(0,240,10))>0))>length(find((sum(REWARDS(2:end))-linspace(0,240,10))>0)))&&(length(REWARDS)>2)
        set(handles.text1, 'string', 'Milestone achieved!  Please ask for your candy, then press Y to continue...'); 
        axes(handles.axes1);
        bar(0.5, REWARDS(1), 0.2, 'g');
        set(handles.axes1, 'xtick', [], 'ytick', [0 1], 'yticklabel', ['Min';'Max'], 'fontsize', 14);
        xlim([0 1]); ylim([0 1]);
        axes(handles.axes2);
        bar(0.5, sum(REWARDS), 0.2, 'g');    
        set(handles.axes2, 'xtick', [], 'ytick', linspace(0, NUM_TRIALS, 10), 'yticklabel', 0:9, 'fontsize', 14, 'box', 'on');
        xlim([0 1]); ylim([0 NUM_TRIALS]);
        set(hObject, 'keypressfcn', {@keyreward, handles, filename});
    end
else,
    axes(handles.axes1);
    bar(0.5, 0, 0.2, 'r');
    xlim([0 1]); ylim([0 1]);
    set(handles.axes1, 'xtick', [], 'ytick', [0 1], 'yticklabel', ['Min';'Max'], 'fontsize', 14);
    set(handles.text1, 'string', [int2str(length(CHOICES)) ' All Done, Press Q to exit.']); 
    set(hObject, 'keypressfcn', {@keyexit, handles});
    rewards = fliplr(REWARDS);
    choices = fliplr(CHOICES);
    save(filename, 'choices', 'rewards');
end;




function keystart(hObject, eventdata, handles, filename)
if get(hObject, 'currentcharacter')=='y',
    set(hObject, 'keypressfcn', {@keycallback, handles, filename});
    set(handles.text1, 'string', [int2str(0) ' Trials Completed']); 
    set(handles.axes1, 'xtick', [], 'ytick', [0 1], 'yticklabel', ['Min';'Max'], 'fontsize', 14);
    set(handles.axes2, 'xtick', [], 'ytick', linspace(0,1,10), 'yticklabel', 0:9, 'fontsize', 14, 'box', 'on');
end;



function keyreward(hObject, eventdata, handles, filename)
global CHOICES;
if get(hObject, 'currentcharacter')=='y',
    set(hObject, 'keypressfcn', {@keycallback, handles, filename});
    set(handles.text1, 'string', [int2str(sum(CHOICES)) ' Trials Completed']); 
end;


function keyexit(hObject, eventdata, handles)
if get(hObject, 'currentcharacter')=='q',
    close(hObject);
end;

function reward = reward_func()
global METHOD; global CHOICES; global BUFFER;
if length(CHOICES)==1, BUFFER = 0.5*ones(1,40); end;
BUFFER  = [CHOICES(1) BUFFER(1:39)];
x = mean(BUFFER);
BUFFER = x*ones(1,40);
switch METHOD
    case 1
        A = [linspace(.7, .2, 240) linspace(0.24, 0.8, 160)];
        B = [linspace(0.6,0.2, 160) linspace(0.24, .5, 80) 0.2*ones(1, 160)];
    case 2
        B = [linspace(.7, .2, 260) linspace(0.24, 0.8, 140)];
        A = [linspace(0.6,0.2, 140) linspace(0.24, .5, 120) 0.2*ones(1, 140)];
    case 3
        A = [linspace(.7, .2, 280) linspace(0.24, 0.8, 120)];
        B = [linspace(0.6,0.2, 120) linspace(0.24, .5, 160) 0.2*ones(1, 120)];
end

if CHOICES(1),
    reward = A(median([1 round(400*x) 400]));
else
    reward = B(median([1 round(400*x) 400]));    
end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -