%% Code for Figure 3A JEXBIO/2021/242677
% date: April 2021
%%

%% read data

% metadata
meta = readtable('Fig4A_ForceMetadata.csv');

% force data
force.z1 = readtable('Fig4A_ForceZ1.csv'); % front leg
force.z2 = readtable('Fig4A_ForceZ2.csv'); % middle leg
force.z3 = readtable('Fig4A_ForceZ3.csv'); % hind leg

%% plot data
species = unique(meta.Species);
close all; clc
for s = 1:length(species) % Cataglyphis C versus Formica F
    
    % filter for subsetting ant
    filSpecies = strcmp(meta.Species, species(s)); 
    slopes = unique(meta.Slope);
    
    for sl = 1:length(slopes) % slopes
        
        % filter for subsetting slopes
        filSlopes = logical(filSpecies .* (meta.Slope == slopes(sl))); 
        legs = unique(meta.LegNr(filSlopes));
        
        for l = 1:length(legs)
            
            % subsetting legs
            metanr = find(logical(filSlopes .* meta.LegNr==legs(l))==1);
            nrs = meta.nr(metanr);
            
            % matrix for leg-grouped force data
            fm=nan(100,length(nrs)); 
            
            % loop through individual measurements
            for k = 1:length(nrs)
                nr = nrs(k);
                
                % get individual force measurement
                f = force.(['z',num2str(l)]).(['nr',num2str(nr)]);
                
                % group force measurements
                fm(1:100,k) = f;
                
                % plot
                subplot(length(legs),length(species),length(species)*(l-1)+s)              
                plot(f,'color',[0 0 1.0000 0.5000]); hold on
                
            end
            
            % download stdshade.m from https://de.mathworks.com/matlabcentral/fileexchange/29534-stdshade
            % Simon Musall (2020). stdshade (https://www.mathworks.com/matlabcentral/fileexchange/29534-stdshade), MATLAB Central File Exchange.
            % adjust astd = 1.96.*nanstd(amatrix,[],1)/sqrt(size(amatrix,1)); % 95% confidence interval
            stdshade(fm',0.3,'b'); hold all
            title([species{s},num2str(slopes(sl)),'-leg',num2str(legs(l))])
            axis square
            axis([0 100 -0.6 0.8])
            grid on
        end
    end
end