%Plots data for publication
%takes x and y pairs and plots lines
%optional final arguments:
%'lines'    plots lines (default)
%'line-marker' plots odd pairs as lines, even as matching markers
%'marker'   plots markers
%do not mess with variable names: it'll screw up pprplotyy
function [Hout, AXout]=pprplot(x, y, varargin)

%constants:
%default color order
colororder = (1/255)*[...
    38 38 97;...
    198 161 21;...
    81 101 37;...
    0 137 234;...
    190 12 11;...
    114 30 104];
%font
fontname = 'Times';
%minimum font size
minfont = 12;
%marker order
markerorder = 'ox+sdv';
%paper position
pprpos=[360 278 336 250];
%end constant definition

%read option from text, if any
l_input = length(varargin);
if mod(l_input, 2)
    option_hold = varargin(length(varargin));
    option = cell2mat(option_hold);
    inputs = varargin(1:(length(varargin)-1));
    switch option
        case 'lines'
            opt = 'lines';
        case 'line-marker'
            opt = 'lm';
        case 'marker'
            opt = 'mark';
        otherwise
            error('Unrecognized final argument')
    end
else
    inputs = varargin;
    opt = 'lines';
end


%determine number of things to plot
num_extra_linesx2 = length(inputs);
%data = cell2mat(varargin);
num_lines = 1+num_extra_linesx2/2;

%check first data pair
if (length(x)~=length(y))
    error('Data pair #1 must be vectors of equal lengths')
end
%check subsequent data pairs
if num_lines>1
    for i=1:(num_lines-1)
        x_toplot = cell2mat(varargin(2*i-1));
        y_toplot = cell2mat(varargin(2*i));
        if (length(x_toplot)~=length(y_toplot))
            message = ['Data pair #', num2str(i+1), ' must be vectors of equal lengths'];
            error(message)
        end
    end
end

switch opt
    case 'lines'
        %plot first line
        H(1) = plot(x, y);
        set(H(1), 'Color', colororder(1,:));
        hold on
        for i=2:num_lines
            x_toplot = cell2mat(varargin(2*(i-1)-1));
            y_toplot = cell2mat(varargin(2*(i-1)));
            H(i) = plot(x_toplot, y_toplot);
            set(H(i), 'Color', colororder(i,:));
        end
        hold off
        
        %begin formatting
        set(H, 'LineWidth', 2)
        hold off
    case 'lm'
        %determine number of color-pairs
        num_linepairs = floor(num_lines/2);
        if mod(num_lines, 2)
            warning('Odd number of lines to plot')
            varargin(length(varargin)+1) = {0};
            varargin(length(varargin)+2) = {0};
        end
        H(1) = plot(x,y);
        set(H(1), 'LineWidth', 2);
        hold on
        H(2) = plot(cell2mat(varargin(1)), cell2mat(varargin(2)));
        set([H(1) H(2)], 'Color', colororder(1,:));
        set(H(2), 'LineStyle', 'none')
        set(H(2), 'Marker', markerorder(1));
        set(H(2), 'MarkerFaceColor', colororder(1,:));
        for i=2:num_linepairs
            x_toplot_line = cell2mat(varargin(4*(i-1)-1));
            y_toplot_line = cell2mat(varargin(4*(i-1)));
            x_toplot_marker = cell2mat(varargin(4*(i-1)+1));
            y_toplot_marker = cell2mat(varargin(4*(i-1)+2));
            H(i*2-1) = plot(x_toplot_line, y_toplot_line);
            H(i*2) = plot(x_toplot_marker, y_toplot_marker);
            set([H(i*2-1) H(i*2)], 'Color', colororder(i,:));
            set(H(i*2), 'LineStyle', 'none')
            set(H(i*2), 'Marker', markerorder(i));
            set(H(i*2), 'MarkerFaceColor', colororder(i,:));
        end
        hold off
    case 'mark'
        %plot first line
        H(1) = plot(x, y);
        set(H(1), 'Marker', markerorder(1));
        set(H(1), 'MarkerSize', 5);
        set(H(1), 'MarkerFaceColor', colororder(1,:));
        set(H(1), 'MarkerEdgeColor', colororder(1,:));
        set(H(1), 'LineStyle', 'none')
        hold on
        for i=2:num_lines
            x_toplot = cell2mat(varargin(2*(i-1)-1));
            y_toplot = cell2mat(varargin(2*(i-1)));
            H(i) = plot(x_toplot, y_toplot);
            set(H(i), 'MarkerFaceColor', colororder(i,:));
            set(H(i), 'MarkerEdgeColor', colororder(i,:));
            set(H(i), 'Marker', markerorder(i));
            set(H(i), 'LineStyle', 'none')
        end
        hold off
        
        %begin formatting
        set(H, 'LineWidth', 2)
        hold off
end
%account for 1-line case
if num_lines>1
    axes = cell2mat(get(H, 'Parent'));
    figurenum = cell2mat(get(axes, 'Parent'));
    labels = [cell2mat(get(axes, 'Xlabel')); cell2mat(get(axes, 'Ylabel')); cell2mat(get(axes, 'Zlabel'))];
    titles = cell2mat(get(axes, 'Title'));
else
    axes = get(H, 'Parent');
    figurenum = get(axes, 'Parent');
    labels = [get(axes, 'Xlabel'); get(axes, 'Ylabel'); get(axes, 'Zlabel')];
    titles = get(axes, 'Title');
end
set([axes; labels; titles], 'FontName', fontname)
set(axes, 'FontSize', minfont)
set(labels, 'FontSize', minfont+1);
set(titles, 'FontSize', minfont+1);
%set background to white, size to 3.5 inches x 2.6 inches
set(figurenum, 'Color', [1 1 1])
set(figurenum, 'Position', pprpos)
set(figurenum, 'PaperPositionMode', 'auto');

%outputs
Hout = H;
AXout = axes(1);
end