% 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';
    'Z:/jon\2013_02_14_wingnut-22567-1-minute-clamps/2013-02-14_wingnut_22567_one-minute-clamp_20-trials_37_trial17.mat';
    'Z:/jon/2013_02_26_Yam-22565_1-minute-clamp/2013-02-26_yam_22565_one-minute-clamp_06.mat'
    };


col = [ [0 100 255];
        [255 205 41]]/255;
sym1 = {'o','^','s','d','h'};

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]);
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,'YAxisLocation','left','TickDir','out','FontName','Arial','FontSize',10,'TickLength',[0.025 0.025])
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);
ctl_1 = nan(length(fids),11);
ctl_2 = 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},'MarkerSize',5)
    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
    ctl_1(f,:) = avg_perf.uctl1(avg_perf.steps <=10);
    ctl_2(f,:) = avg_perf.uctl2(avg_perf.steps <=10);
    
    
    
end

%%
% plot(ax3,avg_perf.steps(avg_perf.steps <=10), ctl_1,'o','Color',col(1,:),'MarkerSize',4)
% plot(ax3,avg_perf.steps(avg_perf.steps <=10), ctl_2,'o','Color',col(2,:),'MarkerSize',4)

% errorbar(ax3,avg_perf.steps(avg_perf.steps <=10), mean(ctl_1), std(ctl_1),'Color',col(1,:))
% errorbar(ax3,avg_perf.steps(avg_perf.steps <=10), mean(ctl_2), std(ctl_2),'Color',col(2,:))

plot(ax3,avg_perf.steps(avg_perf.steps <=10), mean(ctl_1),'Color',col(1,:),'LineWidth',2)
plot(ax3,avg_perf.steps(avg_perf.steps <=10), max(ctl_1),'Color',col(1,:))
plot(ax3,avg_perf.steps(avg_perf.steps <=10), min(ctl_1),'Color',col(1,:))

plot(ax3,avg_perf.steps(avg_perf.steps <=10), mean(ctl_2),'Color',col(2,:),'LineWidth',2)
plot(ax3,avg_perf.steps(avg_perf.steps <=10), max(ctl_2),'Color',col(2,:))
plot(ax3,avg_perf.steps(avg_perf.steps <=10), min(ctl_2),'Color',col(2,:))

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

export_fig -pdf -transparent 1-minute-clamp_temporal-avg-summary_vert_5-culture_02.pdf
