% Plot_Extrusion - Matlab script to plot extruder #3 pressures
  % Written by David Kazmer, January 19, 2024

% Get files names and set styles
sFiles=dir('*.xls');

% Define plot size (please don't change after plotting)
figure(1);clf
x0=300;y0=300;
width=600;height=280;

for iFile=1:length(sFiles)
    clear t d
    sFileName=sFiles(iFile).name;
    T=readtable(sFileName,'VariableNamingRule','preserve');

    % Convert time strings to date numbers
    for i=1:size(T,1)
        t(i)=datenum(T.(1)(i));
    end

    % Convert table to matrix, take middle third of data, and calc Z-scores
    d=[t' table2array(T(:,2:end))];

    % Redefine t as time in minutes from start
    t_s=24*3600*(d(:,1)-d(1,1));

    % Plot 3 x 2 matrix for extruders assuming #2,#3,#4 are running
    % Also assume that die pressure is in EX#5
    set(gcf,'position',[x0,y0,width,height])
    subplot(1,2,iFile);hold on;
    if iFile==1,ylabel('Melt pressure, bar');end
    % Only plot signals if screws are rotating
    if iFile==1 % PP1
        plot(t_s,d(:,30),'ks','MarkerFaceColor','w','DisplayName','Extruder, PP1')
        plot(t_s,d(:,52),'k^','MarkerFaceColor','w','DisplayName','Die, PP1');
    else
        plot(t_s,d(:,30),'rs','MarkerFaceColor','k','DisplayName','Extruder, PP2')
        plot(t_s,d(:,52),'r^','MarkerFaceColor','k','DisplayName','Die, PP2');
    end
    grid on;
    xlabel('Time, s');
    xlim([0 3350]);ylim([0 50])
    legend('location','SouthEast')

    if iFile==1
        p1=d(:,52);
    else
        p2=d(:,52);
    end
    % Calculate some pressure statistics
    mean_pressure_Extruder(iFile)=nanmean(d(:,30))
    sd_pressure_Extruder(iFile)=nanstd(d(:,30))
    mean_pressure_Die(iFile)=nanmean(d(:,52))
    sd_pressure_Die(iFile)=nanstd(d(:,52))

end

[h,p,CI,STATS]=ttest2(p1,p2,'vartype','unequal')

Pres_Rtio=mean_pressure_Die(2)/mean_pressure_Die(1)
MFR_Ratio=8.63/5.67