function varargout = working_memory(varargin)
% WORKING_MEMORY M-file for working_memory.fig
%      WORKING_MEMORY, by itself, creates a new WORKING_MEMORY or raises the existing
%      singleton*.
%
%      H = WORKING_MEMORY returns the handle to a new WORKING_MEMORY or the handle to
%      the existing singleton*.
%
%      WORKING_MEMORY('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in WORKING_MEMORY.M with the given input arguments.
%
%      WORKING_MEMORY('Property','Value',...) creates a new WORKING_MEMORY or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before working_memory_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to working_memory_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help working_memory

% Last Modified by GUIDE v2.5 07-May-2006 11:33:51

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @working_memory_OpeningFcn, ...
                   'gui_OutputFcn',  @working_memory_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(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 working_memory is made visible.
function working_memory_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to working_memory (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes working_memory wait for user response (see UIRESUME)
% uiwait(handles.figure1);

handles.reps = '5';
handles.dsptime = '.9';
handles.intertime = '.5';
handles.delay = .2;
handles.location = [];
handles.grid = 4;
guidata(hObject,handles);
replot(handles)
rand('state',sum(100*clock));

%%
function replot(handles,X,Y)
% clf
N = handles.grid;
x = 0:(N-1);
y = [];
for i = x
    y = [y i*ones(1,N)];
end
x=repmat(x,1,N);
% y = [0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3];
plot(x,y,'o')
xlim([-.5 N-.5])
ylim([-.5 N-.5])
axis off
if nargin > 1
hold on
plot(X,Y,'ro','MarkerSize',10,'MarkerFaceColor','r')
hold off
end

function replot2(handles,X,Y)
% clf
N = handles.grid;
x = 0:(N-1);
y = [];
for i = x
    y = [y i*ones(1,N)];
end
x=repmat(x,1,N);
% y = [0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3];
plot(x,y,'o')
xlim([-.5 N-.5])
ylim([-.5 N-.5])
axis off
if nargin > 1
hold on
plot(X,Y,'bo','MarkerSize',10,'MarkerFaceColor','b')
hold off
end

function replot_errors(handles,X1,Y1,X2,Y2)
% X and Y should be matrices now
% clf
N = handles.grid;
% x = 0:(N-1);
% y = [];
% for i = x
%     y = [y i*ones(1,N)];
% end
% x=repmat(x,1,N);
[x,y] = meshgrid(0:(N-1));
% y = [0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3];
% hold off
plot(x,y,'bo')
xlim([-.5 N-.5]); ylim([-.5 N-.5])
axis off
if nargin > 1
hold on
plot(X1,Y1,'ro','MarkerSize',10,'MarkerFaceColor','r')
plot(X2,Y2,'bs','MarkerSize',5,'MarkerFaceColor','b')
hold off
end

%%
function radioButton(hObject,A,handles)
% handles.location = [handles.location A'];
g = [handles.location A'];
handles.location = g;
guidata(hObject,handles);
% replot(A(1),A(2));
u = timer('TimerFcn','disp('''')','StartDelay',handles.delay);
start(u);wait(u);
set(hObject,'Value',0)
rp = handles.reps;
sz = size(g);
plot(1,1,'')
axis off
text(0,1,['you have answered ' num2str(sz(2)) ' of ' rp])

% --- Outputs from this function are returned to the command line.
function varargout = working_memory_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton1
A = [0 3];
radioButton(hObject,A,handles);


% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton2
A = [1 3];
radioButton(hObject,A,handles);


% --- Executes on button press in radiobutton3.
function radiobutton3_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton3
A = [2 3];
radioButton(hObject,A,handles);


% --- Executes on button press in radiobutton4.
function radiobutton4_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton4
A = [3 3];
radioButton(hObject,A,handles);


% --- Executes on button press in radiobutton5.
function radiobutton5_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton5
A = [0 2];
radioButton(hObject,A,handles);

% --- Executes on button press in radiobutton6.
function radiobutton6_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton6
A = [1 2];
radioButton(hObject,A,handles);


% --- Executes on button press in radiobutton7.
function radiobutton7_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton7
A = [2 2];
radioButton(hObject,A,handles);

% --- Executes on button press in radiobutton8.
function radiobutton8_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton8
A = [3 2];
radioButton(hObject,A,handles);

% --- Executes on button press in radiobutton9.
function radiobutton9_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton9
A = [0 1];
radioButton(hObject,A,handles);


% --- Executes on button press in radiobutton10.
function radiobutton10_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton10
A = [1 1];
radioButton(hObject,A,handles);


% --- Executes on button press in radiobutton11.
function radiobutton11_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton11 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton11
A = [2 1];
radioButton(hObject,A,handles);


% --- Executes on button press in radiobutton12.
function radiobutton12_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton12 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton12
A = [3 1];
radioButton(hObject,A,handles);


% --- Executes on button press in radiobutton13.
function radiobutton13_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton13 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton13
A = [0 0];
radioButton(hObject,A,handles);


% --- Executes on button press in radiobutton14.
function radiobutton14_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton14 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton14
A = [1 0];
radioButton(hObject,A,handles);


% --- Executes on button press in radiobutton15.
function radiobutton15_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton15 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton15
A = [2 0];
radioButton(hObject,A,handles);


% --- Executes on button press in radiobutton16.
function radiobutton16_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton16 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton16
A = [3 0];
radioButton(hObject,A,handles);


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%% main routine
rp = str2num(handles.reps);
if rp <= 0 | (round(rp) ~= rp)
    plot(1,1,'')
    axis off
    text(0,1,['Please enter an integer'
              'greater than 0         ']);
    return;
end
N = rp;
r = handles.grid;
q=round(r*rand(2,N)-1/2);

dt = str2num(handles.dsptime);
ds = str2num(handles.intertime);
if dt <= 0 || dt > 50 || ds <= 0 || ds > 150
    plot(1,1,'')
    axis off
    text(0,1,['Please enter an integer'
              'greater than 0         ']);
    return;
end   
t = timer('TimerFcn','disp('''')','StartDelay',dt);
u = timer('TimerFcn','disp('''')','StartDelay',ds);
replot(handles); start(u);wait(u);

for i = 1:N
    replot(handles,q(1,i),q(2,i));  start(t); wait(t);
    replot(handles);               start(u);wait(u); 
end

handles.location = [];
guidata(hObject,handles);

hold on
plot(1,1,'')
% axis off
text(0,1,['Now enter your choices  '
          'in the grid on the right']);
hold off
t = timer('TimerFcn','disp('''')','StartDelay',.5);
start(t); wait(t);

% g=0; n = 0; nmax = 2000;
% t = timer('TimerFcn','disp('''')','StartDelay',.5);
% while g < N & n < nmax
%     g = handles.location;
%     g = size(g);
%     g = g(2)
%     start(t); wait(t);
%     n = n+1;
% end
% ans = handles.location;
Ans = [];
replot(handles)
for i = 1:N
    [x,y] = ginput(1);
    x = round(x); y = round(y);
    replot2(handles,x,y);
    Ans = [Ans [x,y]'];
end
% 
% disp('correct')
% disp(q)
% disp('your input')
% disp(Ans)
% disp('errors:')
err = sum(abs(q-Ans));
% disp(err)
% count number of errors
numErr = N;
for i = 1:N
    if err(i) ~= 0
        numErr = numErr-1;
    end
end

replot_errors(handles,q(1,:),q(2,:),Ans(1,:),Ans(2,:));
hold on
plot(1,1,'')
% axis off
text(-.2,-.5,['You correctly answered ' num2str(numErr) ' of ' num2str(N)]);
% text(.5,-.5,['click START to play again']);
hold off


%%
function numb_Callback(hObject, eventdata, handles)
% hObject    handle to numb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of numb as text
%        str2double(get(hObject,'String')) returns contents of numb as a double
handles.reps = get(hObject,'String');
guidata(hObject,handles);


% --- Executes during object creation, after setting all properties.
function numb_CreateFcn(hObject, eventdata, handles)
% hObject    handle to numb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function disptime_Callback(hObject, eventdata, handles)
% hObject    handle to disptime (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of disptime as text
%        str2double(get(hObject,'String')) returns contents of disptime as a double
handles.dsptime = get(hObject,'String');
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function disptime_CreateFcn(hObject, eventdata, handles)
% hObject    handle to disptime (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function intertime_Callback(hObject, eventdata, handles)
% hObject    handle to intertime (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of intertime as text
%        str2double(get(hObject,'String')) returns contents of intertime as a double
handles.intertime = get(hObject,'String');
guidata(hObject,handles);


% --- Executes during object creation, after setting all properties.
function intertime_CreateFcn(hObject, eventdata, handles)
% hObject    handle to intertime (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in close_pushbutton.
function close_pushbutton_Callback(hObject, eventdata, handles)
% hObject    handle to close_pushbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% % to pass to the modal dialog.
% pos_size = get(handles.figure1,'Position');
% % Call modaldlg with the argument 'Position'.
% user_response = modaldlg('Title','Confirm Close');
% switch user_response
% case {'No'}
%     % take no action
% case 'Yes'
%     % Prepare to close GUI application window
%     %                  .
%     %                  .
%     %                  .
%     delete(handles.figure1)
% end
delete(handles.figure1)



function grid_edit_text_Callback(hObject, eventdata, handles)
% hObject    handle to grid_edit_text (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of grid_edit_text as text
%        str2double(get(hObject,'String')) returns contents of grid_edit_text as a double
handles.grid = str2num(get(hObject,'String'));
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function grid_edit_text_CreateFcn(hObject, eventdata, handles)
% hObject    handle to grid_edit_text (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


