%Homework 6 Solution

clear all
close all

load rtData
%Load in the variable 'sub' is a 1x7 structure with three fields: 'name',
%'gender' and 'rt' which contains the 10 response times for each subject.

%% Pull the data out of the structure
% Creates a cell array of strings containing the gender for each subject.
gender = {sub.gender};

% Find the values of the structure sub which are 'male'  or 'female
ismale = strcmp(gender,'male');
isfemale = strcmp(gender,'female');

% Create a 7x10 array of RTs
RTs = zeros(length(sub),10);
for s=1:length(sub)
    for i=1:10
        RTs(s,i) = sub(s).rt(i);
    end
end

% Create a new 7x10 array of RTs dividing by the first RT

firstRT = RTs(:,1);
firstRTmat = repmat(firstRT,1,10);

NRTs = RTs./firstRTmat;

%% Set up cell array of strings listing trials for x-axis, and title strings

trStr={'T1', 'T2', 'T3','T4', 'T5', 'T6','T7', 'T8', 'T9','T10'};
titlestr1={'All Ss', 'M & F Ss', 'All Ss', 'M & F Ss'};
titlestr2={'', '', 'Normalized', 'Normalized'};
clrstr={'k', 'b', 'r'};

%% Plot the data

figure(1)
clf
set(gcf,'Position',[10    50   750   650]);

for p=1:4
    subplot(2, 2, p)
    switch p
        case 1
            %Subplot 1:  Plot the average and SEM of the RTs for each of
            %the 10 trials, averaged across all 7 subjects.
            h=errorbar(1:10, mean(RTs), std(RTs)./sqrt(length(sub))); hold on
            set(h, 'Marker', '.', 'MarkerSize', 30, 'Color' ,'k');
        case 2
            %Subplot 2: Plot the average and SEM of the RTs for each of the
            %10 trials, averaged separately for male and female subjects.
            h1=errorbar(1:10, mean(RTs(ismale,:)), std(RTs(ismale,:))./sqrt(sum(ismale))); hold on
            set(h1, 'Marker', '.', 'MarkerSize', 30, 'Color' ,'b');
            h2=errorbar(1:10, mean(RTs(isfemale,:)), std(RTs(isfemale,:))./sqrt(sum(isfemale)));
            set(h2, 'Marker', '.', 'MarkerSize', 30, 'Color' ,'r');
            legend({'Male','Female'},'Location','SouthEast');
        case 3
            %Subplots 3&4 are just like subplots 1 and 2 respectively,
            %except that you should plot the mean and SEM of the RTs after
            %normalizing each subjects’ data by dividing by the RT on the
            %first trial. 
            h=errorbar(1:10, mean(NRTs), std(NRTs)./sqrt(length(sub))); hold on
            set(h, 'Marker', '.', 'MarkerSize', 30, 'Color' ,'k');
        case 4
            h1=errorbar(1:10, mean(NRTs(ismale,:)), std(NRTs(ismale,:))./sqrt(sum(ismale))); hold on
            set(h1, 'Marker', '.', 'MarkerSize', 30, 'Color' ,'b');
            h2=errorbar(1:10, mean(NRTs(isfemale,:)), std(NRTs(isfemale,:))./sqrt(sum(isfemale)));
            set(h2, 'Marker', '.', 'MarkerSize', 30, 'Color' ,'r');
            legend({'Male','Female'},'Location','SouthEast');
    end
    
    %set some parameters general to all four subplots
    if p<3  %y limits for subplots 1-3
        set(gca, 'YLim', [250 500])
    else %y limits for subpplot 4
        set(gca, 'YLim', [.9 1.2])
        plot(1:10,ones(1, 10), 'k--');
    end
    
    %title, x and y labels, x limits and x tick labels
    title([titlestr1{p}, ' ', titlestr2{p}])
    xlabel('Trial Number');
    ylabel('RT');
    set(gca, 'XLim', [0 11])
    set(gca, 'XTick', 1:10);
    set(gca, 'XTickLabel', trStr);
end






