%% Script to import and analyse data from jsPsych online experiment

clc; clear;
set(0,'DefaultFigureWindowStyle','docked');

% subno = input('Subject No');

%% Import data from text file
% Script for importing data from the following text file:
%
%    filename: /Users/riccihannah/Google Drive/Online_Stop_Signal/jspsych-psychophysics-master/Data/All_StopChange_Data_5h6q.csv
%
% Auto-generated by MATLAB on 12-Mar-2021 14:15:50

%% Setup the Import Options
opts = delimitedTextImportOptions("NumVariables", 42);

% Specify range and delimiter
opts.DataLines = [1, Inf];
opts.Delimiter = ",";

% Specify column names and types
opts.VariableNames = ["Var1", "Var2", "Var3", "Var4", "Var5", "Var6", "Var7", "Var8", "Var9", "Var10", "Var11", "Var12", "Var13", "Var14", "Var15", "Var16", "Var17", "Var18", "Var19", "Var20", "Var21", "Var22", "Var23", "Var24", "Var25", "Var26", "Var27", "Var28", "Var29", "Var30", "Var31", "Var32", "Var33", "Var34", "Var35", "Var36", "Var37", "MouseX", "MouseY", "MouseTime", "Var41", "Var42"];
opts.SelectedVariableNames = ["MouseX", "MouseY", "MouseTime"];
opts.VariableTypes = ["string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string"];
opts = setvaropts(opts, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42], "WhitespaceRule", "preserve");
opts = setvaropts(opts, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42], "EmptyFieldRule", "auto");
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";

% Import the data
data = readtable("/Users/riccihannah/Google Drive/Online_Stop_Signal/jspsych-psychophysics-master/Data/All_StopChange_Data_5h6q.csv", opts);

%% data to output type
data = table2array(data);

%% Clear temporary variables
clear opts

%% for each cell
containcomma = find(contains(data(:,1),',')); % find cells with comma
data_new = data(containcomma, :);

mousepos = NaN(length(data_new),100,size(data_new,2));
for c = 1:size(data_new,2)
    coldat = data_new(:, c)
    for i = 1:length(coldat)
        celldat = coldat(i, 1)
        celldatasplit = strsplit(celldat,',')
        celldatadoub = str2double(celldatasplit)
        %     if ~isnan(celldatadoub)
        mousepos(i,1:length(celldatadoub),c) = celldatadoub(:)
        %     end
    end
end

%%
figure()
for i = 1:length(mousepos)
plot(mousepos(i,1:20,1),mousepos(i,1:20,2),'x-')
hold on
end


%%

data = textscan(fid,[repmat('%*q',1,37),repmat('%s',1,100),'%*[^\n]'],'HeaderLines',4,'Delimiter',',','CollectOutput',1)
