%% Load data.

close all; clear; clc

%-----------------------------------------------
% Specify the name of the COMPASS root directory
dataPath='/path/to/COMPASS/repository/';
%-----------------------------------------------
addpath(genpath([dataPath 'Scripts/']))

[~, txtData, allData]=xlsread([dataPath 'Data/COMPASS_SceneInfo.xlsx']); 
header=txtData(1,:); sceneInfo=cell2table(allData(2:end,:),'VariableNames',header);

[~, txtData, allData]=xlsread([dataPath 'Data/COMPASS_RawRatings.xlsx']); 
header=txtData(1,:);sceneRatings=cell2table(allData(2:end,:),'VariableNames',header);

[numData, txtData, allData]=xlsread([dataPath 'Data/COMPASS_Demographics.xlsx']); 
header=txtData(1,:); masterFile=cell2table(allData(2:end,:),'VariableNames',header);

%% Prepare data.

% Exclude participants with low variability in ratings or otherwise 
% questionable data from the demographics masterfile and from scene
% ratings.

excludePpts=masterFile.Subject(masterFile.Include==0);
masterFile(masterFile.Include==0,:)=[];

inclInd=ones(height(sceneRatings),1);
for ii=1:length(excludePpts)
    inclInd(sceneRatings.Subject==excludePpts(ii))=0;
end
sceneRatings=sceneRatings(logical(inclInd),:);

fprintf('Total participants n=%d \n\n',...
    numel(unique(sceneRatings.Subject(~isnan(sceneRatings.Subject))))) 

%% Create subjects x images tables with valence and arousal ratings and RTs. 

images=sceneInfo.ImageFile;
imageNames=sceneInfo.ImageName;
subjects=unique(masterFile.Subject);
valRTs=nan(numel(subjects),numel(images));
arousRTs=nan(numel(subjects),numel(images));
valRatings=nan(numel(subjects),numel(images));
arousRatings=nan(numel(subjects),numel(images));

for ii=1:numel(subjects)
    for jj=1:numel(images)
        ind=sceneRatings.Subject==subjects(ii) & strcmp(sceneRatings.Image,images{jj});
        if sum(ind)==1
            valRTs(ii,jj)=sceneRatings.ValenceRT(ind);
            arousRTs(ii,jj)=sceneRatings.ArousalRT(ind);            
            valRatings(ii,jj)=sceneRatings.ValenceRESP(ind);
            arousRatings(ii,jj)=sceneRatings.ArousalRESP(ind);            
        elseif sum(ind)>1 % Some people rated some scenes twice; use the first rating only 
            temp=sceneRatings.ValenceRT(ind); valRTs(ii,jj)=temp(1);
            temp=sceneRatings.ArousalRT(ind); arousRTs(ii,jj)=temp(1);
            temp=sceneRatings.ValenceRESP(ind); valRatings(ii,jj)=temp(1);
            temp=sceneRatings.ArousalRESP(ind); arousRatings(ii,jj)=temp(1);
        end
    end
end

clearvars valRatings_table arousRatings_table valRTs_table arousRTs_table
valRatings_table.Subject=subjects;valRatings_table=struct2table(valRatings_table);
arousRatings_table.Subject=subjects;arousRatings_table=struct2table(arousRatings_table);
valRTs_table.Subject=subjects;valRTs_table=struct2table(valRTs_table);
arousRTs_table.Subject=subjects;arousRTs_table=struct2table(arousRTs_table);

for ii=1:numel(imageNames)
    valRatings_table.(imageNames{ii})=valRatings(:,ii);
    arousRatings_table.(imageNames{ii})=arousRatings(:,ii);
    valRTs_table.(imageNames{ii})=valRTs(:,ii);
    arousRTs_table.(imageNames{ii})=arousRTs(:,ii);
end
valRatings_table.Gender=categorical(masterFile.Gender);
arousRatings_table.Gender=categorical(masterFile.Gender);
valRTs_table.Gender=categorical(masterFile.Gender);
arousRTs_table.Gender=categorical(masterFile.Gender);

writetable(valRatings_table,[dataPath 'Data/COMPASS_ValenceRatings.xlsx'])
writetable(arousRatings_table,[dataPath 'Data/COMPASS_ArousalRatings.xlsx'])
writetable(valRTs_table,[dataPath 'Data/COMPASS_ValenceRTs.xlsx']);
writetable(arousRTs_table,[dataPath 'Data/COMPASS_ArousalRTs.xlsx']);

%% Calculate summary statistics of scene-wise valence and arousal ratings.

% Exclude bad trials (<150 ms or >4000 ms).

exclValTrials=valRTs>4000 | valRTs<150;
exclArousTrials=arousRTs>4000 | arousRTs<150;

fprintf('Excluded %d (%.1f%%) valence ratings \n',sum(sum(exclValTrials)),...
    sum(sum(exclValTrials))/numel(exclValTrials)*100) 
fprintf('Excluded %d (%.1f%%) arousal ratings \n\n',sum(sum(exclArousTrials)),...
    sum(sum(exclArousTrials))/numel(exclArousTrials)*100) 

valRatings(logical(exclValTrials))=NaN; 
arousRatings(logical(exclArousTrials))=NaN; 

clearvars meanRatings
meanRatings.Image=images;meanRatings=struct2table(meanRatings);
meanRatings.ImageName=sceneInfo.ImageName;
meanRatings.Name=sceneInfo.Name;
meanRatings.ValenceMean=nanmean(valRatings)';
meanRatings.ValenceSD=nanstd(valRatings)';	
meanRatings.ArousalMean=nanmean(arousRatings)';	
meanRatings.ArousalSD=nanstd(arousRatings)';	
meanRatings.NumValRatings=sum(~isnan(valRatings))';	
meanRatings.NumArousRatings=sum(~isnan(arousRatings))';

fprintf('After exclusions, each image retained valence ratings by an average of %.0f',...
    nanmean(meanRatings.NumValRatings))
fprintf(' participants \n(SD = %.0f, range %.0f-%.0f) and arousal ratings ',...
    nanstd(meanRatings.NumValRatings),nanmin(meanRatings.NumValRatings),...
    nanmax(meanRatings.NumValRatings))
fprintf('by an average of %.0f participants \n(SD = %.0f, range %.0f-%.0f). \n\n',...
    nanmean(meanRatings.NumArousRatings),nanstd(meanRatings.NumArousRatings),...
    nanmin(meanRatings.NumArousRatings),nanmax(meanRatings.NumArousRatings))

fprintf('Valence M = %.2f (SD = %.2f)\n',nanmean(meanRatings.ValenceMean),...
    nanstd(meanRatings.ValenceMean))

ind=meanRatings.ValenceMean==nanmin(meanRatings.ValenceMean);
fprintf('Min Valence M = %.2f (SD = %.2f) %s %s \n',meanRatings.ValenceMean(ind),...
    meanRatings.ValenceSD(ind),meanRatings.ImageName{ind},meanRatings.Name{ind})

ind=meanRatings.ValenceMean==nanmax(meanRatings.ValenceMean);
fprintf('Max Valence M = %.2f (SD = %.2f) %s %s \n',meanRatings.ValenceMean(ind),...
    meanRatings.ValenceSD(ind),meanRatings.ImageName{ind},meanRatings.Name{ind})

fprintf('SD Valence M = %.2f (SD = %.2f)\n',nanmean(meanRatings.ValenceSD),...
    nanstd(meanRatings.ValenceSD))

ind=meanRatings.ValenceSD==nanmin(meanRatings.ValenceSD);
fprintf('Min SD Valence M = %.2f (SD = %.2f) %s %s \n',meanRatings.ValenceMean(ind),...
    meanRatings.ValenceSD(ind),meanRatings.ImageName{ind},meanRatings.Name{ind})

ind=meanRatings.ValenceSD==nanmax(meanRatings.ValenceSD);
fprintf('Max SD Valence M = %.2f (SD = %.2f) %s %s \n\n',meanRatings.ValenceMean(ind),...
    meanRatings.ValenceSD(ind),meanRatings.ImageName{ind},meanRatings.Name{ind})

fprintf('Arousal M = %.2f (SD = %.2f)\n',nanmean(meanRatings.ArousalMean),...
    nanstd(meanRatings.ArousalMean))

ind=meanRatings.ArousalMean==nanmin(meanRatings.ArousalMean);
fprintf('Min Arousal M = %.2f (SD = %.2f) %s %s \n',meanRatings.ArousalMean(ind),...
    meanRatings.ArousalSD(ind),meanRatings.ImageName{ind},meanRatings.Name{ind})

ind=meanRatings.ArousalMean==nanmax(meanRatings.ArousalMean);
fprintf('Max Arousal M = %.2f (SD = %.2f) %s %s \n',meanRatings.ArousalMean(ind),...
    meanRatings.ArousalSD(ind),meanRatings.ImageName{ind},meanRatings.Name{ind})

fprintf('SD Arousal M = %.2f (SD = %.2f)\n',nanmean(meanRatings.ArousalSD),...
    nanstd(meanRatings.ArousalSD))

ind=meanRatings.ArousalSD==nanmin(meanRatings.ArousalSD);
fprintf('Min SD Arousal M = %.2f (SD = %.2f) %s %s \n',meanRatings.ArousalMean(ind),...
    meanRatings.ArousalSD(ind),meanRatings.ImageName{ind},meanRatings.Name{ind})

ind=meanRatings.ArousalSD==nanmax(meanRatings.ArousalSD);
fprintf('Max SD Arousal M = %.2f (SD = %.2f) %s %s \n\n',meanRatings.ArousalMean(ind),...
    meanRatings.ArousalSD(ind),meanRatings.ImageName{ind},meanRatings.Name{ind})

fprintf('Skewness Valence Ratings %.2f \n',skewness(meanRatings.ValenceMean))
fprintf('Skewness Arousal Ratings %.2f \n\n',skewness(meanRatings.ArousalMean))
fprintf('Skewness Valence SD %.2f \n',skewness(meanRatings.ValenceSD))
fprintf('Skewness Arousal SD %.2f \n\n',skewness(meanRatings.ArousalSD))

%% Make Figure S1.
%  Scene-wise heatmaps of valence and arousal ratings.

r=linspace(.96,.4,20);g=linspace(.96,.4,20);b=linspace(.99,.8,20);
c=[r' g' b']; fontSize=12;

for ii=1:numel(images)
    f=figure('color','w');
    clearvars imgRatings
    imgRatings.Val=valRatings(:,ii); imgRatings=struct2table(imgRatings);
    imgRatings.Arous=arousRatings(:,ii);
    h=heatmap(imgRatings,'Val','Arous');
    h.GridVisible='off'; set(gca,'FontSize',fontSize)
    h.SourceTable.Arous=categorical(h.SourceTable.Arous);
    neworder=fliplr({'1','2','3','4','5','6','7','8','9'});
    h.SourceTable.Arous=reordercats(h.SourceTable.Arous,neworder);shg
    h.XLabel='Valence';h.YLabel='Arousal'; h.Title=imageNames{ii};h.Colormap=c;
    f.Position=[360 434 344 264];
    figureName=[dataPath 'Figures/Scene_Ratings_Fig' num2str(ii)];
    print(figureName,'-dpng'); close(f)
end

% NOTE: The below commands require FSL installation. 
[~,fslPath]=unix('echo $FSL_DIR');
cd([dataPath 'Figures/'])
for ii=1:5:numel(images)
    cmd=sprintf('%s/bin/pngappend %s + %s + %s + %s + %s Fig%d.png',fslPath(2:end-1),...
        ['Scene_Ratings_Fig' num2str(ii) '.png'],['Scene_Ratings_Fig' num2str(ii+1) '.png'],...
        ['Scene_Ratings_Fig' num2str(ii+2) '.png'],['Scene_Ratings_Fig' num2str(ii+3) '.png'],...
        ['Scene_Ratings_Fig' num2str(ii+4) '.png'],ii);
    unix(cmd) % String 5 heatmaps horizontally
end
counter=0;
for ii=1:15
    cmd=sprintf('%s/bin/pngappend %s - %s - %s - %s Figure%d.png',fslPath(2:end-1),...
        ['Fig' num2str(counter+1) '.png'],['Fig' num2str(counter+6) '.png'],...
        ['Fig' num2str(counter+11) '.png'],['Fig' num2str(counter+16) '.png'],ii);
    unix(cmd) % String 4 sets of heatmaps vertically
    counter=counter+20;
end

%% Make Figure 1.
%  Distributions of scene-wise means and standard deviations of valence and arousal 
%  ratings.

lineWidth=1.5;fontSize=15;

figure('color','w');subplot(2,2,1)
h=histogram(meanRatings.ValenceMean,'BinWidth',.2);
h.LineWidth=lineWidth; h.FaceColor=[.8 .3 .8];
set(gca,'XTick',[1:9],'LineWidth',lineWidth,'FontSize',fontSize)
xlim([1 9]);box on;axis square;ylim([0 40]);title('Mean Valence Ratings');
xlabel('Mean valence ratings');ylabel('Frequency')

subplot(2,2,2)
h=histogram(meanRatings.ArousalMean,'BinWidth',.2);
h.LineWidth=lineWidth; h.FaceColor=[1 .8 .2];
set(gca,'XTick',[1:9],'LineWidth',lineWidth,'FontSize',fontSize)
xlim([1 9]);box on;axis square;ylim([0 40]);title('Mean Arousal Ratings');
xlabel('Mean arousal ratings');ylabel('Frequency')

subplot(2,2,3)
h=histogram(meanRatings.ValenceSD,'BinWidth',.07);
h.LineWidth=lineWidth; h.FaceColor=[.8 .3 .8];
set(gca,'LineWidth',lineWidth,'FontSize',fontSize)
box on;axis square;xlim([0 3]);ylim([0 50]);title('SD of Valence Ratings');
xlabel('SD of valence ratings');ylabel('Frequency');

subplot(2,2,4)
h=histogram(meanRatings.ArousalSD,'BinWidth',.07);
h.LineWidth=lineWidth; h.FaceColor=[1 .8 .2];
set(gca,'LineWidth',lineWidth,'FontSize',fontSize)
box on;axis square;xlim([0 3]);ylim([0 50]);title('SD of Arousal Ratings');
xlabel('SD of arousal ratings');ylabel('Frequency');

%% Make Figure 2. 
%  The relation between COMPASS valence (1 = unpleasant, 9 = pleasant) and 
%  arousal (1 = low, 9 = high) ratings.

lineWidth=1.5;fontSize=15;
screenSize=get(0,'ScreenSize');
socColor=[1 .6 .1];nonSocColor=[.1 .6 1];

figure('color','w','units','normalized','Position',screenSize);hold on
l1=line([1 9],[5 5],'LineWidth',lineWidth,'Color',[.4 .4 .4]);
l2=line([5 5],[1 9],'LineWidth',lineWidth,'Color',[.4 .4 .4]);

xOverlap=.4; yOverlap=.1;
[xCoord,yCoord]=textSpace(meanRatings.ValenceMean,meanRatings.ArousalMean,...
    xOverlap,yOverlap);
t=text(xCoord+.1,yCoord+.1,sceneInfo.Name,'FontSize',7,'Color',[.2 .2 .2]);

socInd=strcmp(sceneInfo.SocialContent,'Social');
s1=scatter(meanRatings.ValenceMean(socInd),meanRatings.ArousalMean(socInd),...
    'MarkerEdgeColor','none','MarkerFaceColor',socColor,'DisplayName','Social');
s2=scatter(meanRatings.ValenceMean(~socInd),meanRatings.ArousalMean(~socInd),...
    'MarkerEdgeColor','none','MarkerFaceColor',nonSocColor,'DisplayName','Nonsocial');
xlim([1 9]);ylim([1 9]); box on;grid on; axis square
set(gca,'XTick',[1:9],'YTick',[1:9],'LineWidth',lineWidth,'FontSize',fontSize)
xlabel('Valence');ylabel('Arousal'); 
title('Mean Valence & Arousal Ratings');l=legend([s1 s2]);

%% Make Figure 3. 
%  The relations between scene-wise means and standard deviations of COMPASS 
%  valence and arousal ratings.

lineWidth=1.5;fontSize=15;
screenSize=get(0,'ScreenSize');
socColor=[1 .6 .1];nonSocColor=[.1 .6 1];
socInd=strcmp(sceneInfo.SocialContent,'Social');

figure('color','w','units','normalized','Position',screenSize)
subplot(1,2,1);hold on
s1=scatter(meanRatings.ValenceMean(socInd),meanRatings.ValenceSD(socInd),...
    'MarkerEdgeColor','none','MarkerFaceColor',socColor,'DisplayName','Social');
s2=scatter(meanRatings.ValenceMean(~socInd),meanRatings.ValenceSD(~socInd),...
    'MarkerEdgeColor','none','MarkerFaceColor',nonSocColor,'DisplayName','Nonsocial');
xlim([1 9]);ylim([0.5 3.5]);box on;axis square
set(gca,'XTick',[1:9],'LineWidth',lineWidth,'FontSize',fontSize)
xlabel('Valence Mean');ylabel('Valence Standard Deviation'); 
title('Valence Ratings');l=legend([s1 s2]);

subplot(1,2,2);hold on
s1=scatter(meanRatings.ArousalMean(socInd),meanRatings.ArousalSD(socInd),...
    'MarkerEdgeColor','none','MarkerFaceColor',socColor,'DisplayName','Social');
s2=scatter(meanRatings.ArousalMean(~socInd),meanRatings.ArousalSD(~socInd),...
    'MarkerEdgeColor','none','MarkerFaceColor',nonSocColor,'DisplayName','Nonsocial');
xlim([1 9]);ylim([0.5 3.5]);box on;axis square
set(gca,'XTick',[1:9],'LineWidth',lineWidth,'FontSize',fontSize)
xlabel('Arousal Mean');ylabel('Arousal Standard Deviation'); 
title('Arousal Ratings');l=legend([s1 s2]);

%% Make Table 2.
%  Valence and arousal ratings by assigned scene category.

valCat=unique(sceneInfo.Valence); 
arousCat=unique(sceneInfo.Arousal); 
socCat=unique(sceneInfo.SocialContent); 

for ii=1:numel(valCat)
    ind=strcmp(sceneInfo.Valence,valCat{ii});
    ind=repmat(ind',numel(subjects),1);
    clearvars meanResp
    meanResp(1,1)=nanmean(valRatings(ind));
    meanResp(1,2)=nanstd(valRatings(ind));
    meanResp(2,1)=nanmean(arousRatings(ind));
    meanResp(2,2)=nanstd(arousRatings(ind));
    
    fprintf('%s \nValence M (SD)=%.2f (%.2f)\nArousal M (SD)=%.2f (%.2f) \n\n', ...
        valCat{ii}, meanResp(1,1),meanResp(1,2),meanResp(2,1),meanResp(2,2))
end

for ii=1:numel(arousCat)
    ind=strcmp(sceneInfo.Arousal,arousCat{ii});
    ind=repmat(ind',numel(subjects),1);
    clearvars meanResp
    meanResp(1,1)=nanmean(valRatings(ind));
    meanResp(1,2)=nanstd(valRatings(ind));
    meanResp(2,1)=nanmean(arousRatings(ind));
    meanResp(2,2)=nanstd(arousRatings(ind));
    
    fprintf('%s \nValence M (SD)=%.2f (%.2f)\nArousal M (SD)=%.2f (%.2f) \n\n', ...
        arousCat{ii}, meanResp(1,1),meanResp(1,2),meanResp(2,1),meanResp(2,2))
end

for ii=1:numel(socCat)
    ind=strcmp(sceneInfo.SocialContent,socCat{ii});
    ind=repmat(ind',numel(subjects),1);
    clearvars meanResp
    meanResp(1,1)=nanmean(valRatings(ind));
    meanResp(1,2)=nanstd(valRatings(ind));
    meanResp(2,1)=nanmean(arousRatings(ind));
    meanResp(2,2)=nanstd(arousRatings(ind));
    
    fprintf('%s \nValence M (SD)=%.2f (%.2f)\nArousal M (SD)=%.2f (%.2f) \n\n', ...
        socCat{ii}, meanResp(1,1),meanResp(1,2),meanResp(2,1),meanResp(2,2))
end

for ii=1:numel(valCat)
    for jj=1:numel(arousCat)
        ind=strcmp(sceneInfo.Valence,valCat{ii}) & strcmp(sceneInfo.Arousal,arousCat{jj});
        ind=repmat(ind',numel(subjects),1);
        clearvars meanResp
        meanResp(1,1)=nanmean(valRatings(ind));
        meanResp(1,2)=nanstd(valRatings(ind));
        meanResp(2,1)=nanmean(arousRatings(ind));
        meanResp(2,2)=nanstd(arousRatings(ind));
        
        fprintf('%s %s \nValence M (SD)=%.2f (%.2f)\nArousal M (SD)=%.2f (%.2f) \n\n', ...
            valCat{ii},arousCat{jj}, meanResp(1,1),meanResp(1,2),meanResp(2,1),meanResp(2,2))
    end
end

for ii=1:numel(valCat)
    for jj=2:-1:1
        ind=strcmp(sceneInfo.Valence,valCat{ii}) & strcmp(sceneInfo.SocialContent,socCat{jj});
        ind=repmat(ind',numel(subjects),1);
        clearvars meanResp
        meanResp(1,1)=nanmean(valRatings(ind));
        meanResp(1,2)=nanstd(valRatings(ind));
        meanResp(2,1)=nanmean(arousRatings(ind));
        meanResp(2,2)=nanstd(arousRatings(ind));
        
        fprintf('%s %s \nValence M (SD)=%.2f (%.2f)\nArousal M (SD)=%.2f (%.2f) \n\n', ...
            valCat{ii},socCat{jj}, meanResp(1,1),meanResp(1,2),meanResp(2,1),meanResp(2,2))
    end
end

for ii=1:numel(valCat)
    for jj=1:numel(arousCat)
        for hh=2:-1:1
            ind=strcmp(sceneInfo.Valence,valCat{ii}) & ...
                strcmp(sceneInfo.Arousal,arousCat{jj})...
                & strcmp(sceneInfo.SocialContent,socCat{hh});
            ind=repmat(ind',numel(subjects),1);
            clearvars meanResp
            meanResp(1,1)=nanmean(valRatings(ind));
            meanResp(1,2)=nanstd(valRatings(ind));
            meanResp(2,1)=nanmean(arousRatings(ind));
            meanResp(2,2)=nanstd(arousRatings(ind));
            
            fprintf('%s %s %s \nValence M (SD)=%.2f (%.2f)\nArousal M (SD)=%.2f (%.2f) \n\n', ...
                valCat{ii},arousCat{jj},socCat{hh},meanResp(1,1),meanResp(1,2),meanResp(2,1),meanResp(2,2))
        end
    end
end

%% Make subjects x scenes tables with mean ratings by affective category.

clearvars rmVal rmArous
rmVal.Subject=subjects;
rmVal.Gender=categorical(masterFile.Gender);
if isstruct(rmVal)
    rmVal=struct2table(rmVal);
end
rmArous=rmVal;

for ii=1:numel(valCat)
    for jj=1:numel(arousCat)
        for kk=1:numel(socCat)
            ind=strcmp(sceneInfo.Valence,valCat{ii}) & strcmp(sceneInfo.Arousal,arousCat{jj})...
                & strcmp(sceneInfo.SocialContent,socCat{kk});           
            varName=[valCat{ii}(1:3) arousCat{jj} socCat{kk}(1)];
            temp=valRatings(:,ind'); rmVal.(varName)=nanmean(temp,2);
            temp=arousRatings(:,ind'); rmArous.(varName)=nanmean(temp,2);
        end
    end
end

rmVal(:,{'NegLowN','NegLowS'}) = []; 
rmVal(:,{'NeuHighN','NeuHighS'}) = [];
rmVal(:,{'PosLowN','PosLowS'}) = [];
rmArous(:,{'NegLowN','NegLowS'}) = [];
rmArous(:,{'NeuHighN','NeuHighS'}) = [];
rmArous(:,{'PosLowN','PosLowS'}) = [];
rmVal=rmVal(:,[1 3:end 2]);rmArous=rmArous(:,[1 3:end 2]);

writetable(rmVal,[dataPath 'Data/COMPASS_ValenceRatingsByAffect.xlsx'])
writetable(rmArous,[dataPath 'Data/COMPASS_ArousalRatingsByAffect.xlsx'])

%% Make Figure 4. 
%  Mean valence and arousal ratings for each affective scene category 
%  (lower arousal = low to moderate; higher arousal = moderate to high).

lineWidth=1.5;fontSize=15;
screenSize=get(0,'ScreenSize');
socColor=[1 .6 .1];nonSocColor=[.1 .6 1];

figure('color','w'); subplot(1,2,1);hold on
e1=errorbar([1 2 3],[nanmean(rmVal.NegHighS) nanmean(rmVal.NeuMidS) nanmean(rmVal.PosHighS)],...
    [sem(rmVal.NegHighS) sem(rmVal.NeuMidS) sem(rmVal.PosHighS)],'LineWidth',lineWidth,...
    'Color',socColor,'Marker','o','MarkerEdgeColor',socColor,'MarkerFaceColor',socColor,...
    'DisplayName','Social Higher Arousal');hold on
e2=errorbar([1 2 3],[nanmean(rmVal.NegMidS) nanmean(rmVal.NeuLowS) nanmean(rmVal.PosMidS)],...
    [sem(rmVal.NegMidS) sem(rmVal.NeuLowS) sem(rmVal.PosMidS)],'LineWidth',lineWidth,...
    'Color',socColor,'Marker','^','MarkerEdgeColor',socColor,'MarkerFaceColor',socColor,...
    'DisplayName','Social Lower Arousal','LineStyle','--');
e3=errorbar([1 2 3],[nanmean(rmVal.NegHighN) nanmean(rmVal.NeuMidN) nanmean(rmVal.PosHighN)],...
    [sem(rmVal.NegHighN) sem(rmVal.NeuMidN) sem(rmVal.PosHighN)],'LineWidth',lineWidth,...
    'Color',nonSocColor,'Marker','o','MarkerEdgeColor',nonSocColor,'MarkerFaceColor',nonSocColor,...
    'DisplayName','Nonsocial Higher Arousal');
e4=errorbar([1 2 3],[nanmean(rmVal.NegMidN) nanmean(rmVal.NeuLowN) nanmean(rmVal.PosMidN)],...
    [sem(rmVal.NegMidN) sem(rmVal.NeuLowN) sem(rmVal.PosMidN)],'LineWidth',lineWidth,...
    'Color',nonSocColor,'Marker','^','MarkerEdgeColor',nonSocColor,'MarkerFaceColor',nonSocColor,...
    'DisplayName','Nonsocial Lower Arousal','LineStyle','--');
set(gca,'FontSize',fontSize,'LineWidth',lineWidth,'XTick',[1 2 3],'XTickLabel',valCat)
l=legend([e1 e2 e3 e4]);xlim([0 4]); ylim([1 9]);title('Valence Ratings by Scene Category')
ylabel('Mean valence ratings');xlabel('Scene valence');axis square; box on

subplot(1,2,2)
e1=errorbar([1 2 3],[nanmean(rmArous.NegHighS) nanmean(rmArous.NeuMidS) nanmean(rmArous.PosHighS)],...
    [sem(rmArous.NegHighS) sem(rmArous.NeuMidS) sem(rmArous.PosHighS)],'LineWidth',lineWidth,...
    'Color',socColor,'Marker','o','MarkerEdgeColor',socColor,'MarkerFaceColor',socColor,...
    'DisplayName','Social Higher Arousal');hold on
e2=errorbar([1 2 3],[nanmean(rmArous.NegMidS) nanmean(rmArous.NeuLowS) nanmean(rmArous.PosMidS)],...
    [sem(rmArous.NegMidS) sem(rmArous.NeuLowS) sem(rmArous.PosMidS)],'LineWidth',lineWidth,...
    'Color',socColor,'Marker','^','MarkerEdgeColor',socColor,'MarkerFaceColor',socColor,...
    'DisplayName','Social Lower Arousal','LineStyle','--');
e3=errorbar([1 2 3],[nanmean(rmArous.NegHighN) nanmean(rmArous.NeuMidN) nanmean(rmArous.PosHighN)],...
    [sem(rmArous.NegHighN) sem(rmArous.NeuMidN) sem(rmArous.PosHighN)],'LineWidth',lineWidth,...
    'Color',nonSocColor,'Marker','o','MarkerEdgeColor',nonSocColor,'MarkerFaceColor',nonSocColor,...
    'DisplayName','Nonsocial Higher Arousal');
e4=errorbar([1 2 3],[nanmean(rmArous.NegMidN) nanmean(rmArous.NeuLowN) nanmean(rmArous.PosMidN)],...
    [sem(rmArous.NegMidN) sem(rmArous.NeuLowN) sem(rmArous.PosMidN)],'LineWidth',lineWidth,...
    'Color',nonSocColor,'Marker','^','MarkerEdgeColor',nonSocColor,'MarkerFaceColor',nonSocColor,...
    'DisplayName','Nonsocial Lower Arousal','LineStyle','--');

set(gca,'FontSize',fontSize,'LineWidth',lineWidth,'XTick',[1 2 3],'XTickLabel',valCat)
l=legend([e1 e2 e3 e4]);xlim([0 4]); ylim([1 9]);title('Arousal Ratings by Scene Category')
ylabel('Mean arousal ratings');xlabel('Scene valence');axis square; box on
