% Jon Newman

%% file id
fids = { 
    'J:/newmonster/2012_07_02_Xeko-1-minute-clamps/2012_07_02_Xeko-20709_one-min-clamp_07.mat';
    'J:/newmonster/2012_10_11-Herb-I5387-1-minute-clamps/2012_10_11_Herb-I5387_one-min-clamp_02.mat';
    'J:/newmonster/2012_10_22_Herb-I5386-1-minute-clamps/2012_10_22_Herb-I5386_one-min-clamp_10.mat'
    };


col = [[ 0 0 0];
    [0 100 255];
    [220 0 0]]/255;
sym1 = {'o','^','s'};

close all
figure('units','centimeter','position',[40 5 15 20]);

% Average FR
ax1 = axes('units','centimeter','position',[5 10 2.5 2.5]);
hold on;
set(gca,'TickDir','out','FontName','Arial','FontSize',10,'TickLength',[0.025 0.025],'XTick',[]);
plot([0 10],[0 10],'k:')
axis([0 10 0 10])
ylabel('Acheived Rate')

% Average residual
ax2 = axes('units','centimeter','position',[5 7 2.5 2.5]);
set(gca,'TickDir','out','FontName','Arial','FontSize',10,'TickLength',[0.025 0.025],'XTick',[],'YAxisLocation','left')
ylabel('Residual')

% Average CTL
ax3 = axes('units','centimeter','position',[5 4 2.5 2.5]);
hold on;
set(gca,'TickDir','out','FontName','Arial','FontSize',10,'TickLength',[0.025 0.025])
axis([0 10 0 1])
xlabel('Target Rate')
ylabel('u_1 and u_2')

res = nan(length(fids),11);



for f = 1:length(fids)
    
    fid = fids{f};
    load(fid);
    
    %% Tracking
    plot(ax1,avg_perf.steps,avg_perf.ufr,'k','Marker',sym1{f})
    if (size(avg_perf.steps,1) > 1)
         avg_perf.steps =  avg_perf.steps';
    end
    res(f,:) = abs(avg_perf.steps(avg_perf.steps <=10) - avg_perf.ufr(avg_perf.steps <=10)); %./avg_perf.steps(avg_perf.steps <=10);
    
    
    %% Control Signal
    plot(ax3,avg_perf.steps,avg_perf.uctl1,'k','Marker',sym1{f})
    plot(ax3,avg_perf.steps,avg_perf.uctl2,'k','Marker',sym1{f})
    
    
end

%%
semilogy(ax2, avg_perf.steps(avg_perf.steps <=10),mean(res),'k:');
set(ax2,'YTick',[0.001 0.01 0.1 1 10],'Ylim',[1e-3 10]);

% export_fig -pdf -transparent 1-minute-clamp_temporal-avg-summary_vert_01.pdf
