
close all
clc

% Predefinition of cells
xloads1 = cell(n.red,1);
xloads2 = cell(n.red,1);
xloads3 = cell(n.red,1);
xloads4 = cell(n.red,1);
for j = 1:n.red
    xloads1{j} = zeros(n.in,n.PLS);
    xloads2{j} = zeros(n.in,n.PLS);
    xloads3{j} = zeros(n.in,n.PLS);
    xloads4{j} = zeros(n.in,n.PLS);
end
vari = cell(n.red,1);
indi = cell(n.red,1);

% Calculation of the chosen point
for k = 1:n.PLS
    for j = 1:n.red   
        xloads1{j}(:,k) = xloads{k,j}(:,1);
        xloads2{j}(:,k) = xloads{k,j}(:,sumpct{end});
        xloads3{j}(:,k) = xloads{k,j}(:,sumpct{end}+1);
        xloads4{j}(:,k) = xloads{k,j}(:,n.in);
    end
end

for j = 1:n.red
    % Calculation of the variance of the values
    vari{j}.m1 = var(xloads1{j},[],2);
    vari{j}.m2 = var(xloads2{j},[],2);
    vari{j}.m3 = var(xloads3{j},[],2);
    vari{j}.m4 = var(xloads4{j},[],2);

    [~,indi{j}.ma1] = max(vari{j}.m1);
    [~,indi{j}.ma2] = max(vari{j}.m2);
    [~,indi{j}.ma3] = max(vari{j}.m3);
    [~,indi{j}.ma4] = max(vari{j}.m4);

    [~,indi{j}.mi1] = min(vari{j}.m1);
    [~,indi{j}.mi2] = min(vari{j}.m2);
    [~,indi{j}.mi3] = min(vari{j}.m3);
    [~,indi{j}.mi4] = min(vari{j}.m4);
end

% Definition of integer values
itg.fig = 1;
itg.lab = 1;
itg.fda = 1;
itg.axe = 1;
itg.ann = 1;

% Definition of figure and values
fig.dimw12 = 12.3;  % 12.5 for ESCAPE
fig.dimh12 = 5.5;
fig.dimw21 = 8.4;
fig.dimh21 = 12;
fig.dimw = 8.4;
fig.dimh = 6;
fig.Font = [10 8];
fig.label = [];
fig.ann   = [];

% Plot of the evaluation of the value with the maximum load
for j = 1:n.red
    fig.in(itg.fig) = figure(itg.fig);
    set(fig.in(itg.fig),'units','centimeters','position', [5+itg.fig,5+itg.fig, fig.dimw, fig.dimh]);
    yyaxis right
    fig.axes(itg.fig) = gca;
    fig.axes(itg.fig).YAxis(1).Color = 'k';
    fig.axes(itg.fig).YAxis(2).Color = 'r';
    hold on
    fig.dat(itg.fda) = plot(Np.tot(1:n.PLS),(xloads3{j}(indi{j}.mi3,:))-0.5,'r'); itg.fda = itg.fda+1;
    fig.dat(itg.fda) = plot(Np.tot(1:n.PLS),(xloads3{j}(indi{j}.ma3,:))-0.5,'r--'); itg.fda = itg.fda+1;
    fig.label(itg.lab) = ylabel(['${w}_{' num2str(sumpct{end}+1) ',k}$']); itg.lab = itg.lab+1;
    ylim([-1.25 1.5])
    fig.axes(1).YTick = -1.5:0.5:2;
    fig.axes(1).YTickLabel = {'-1.0';'-0.5';'0.0';'0.5';'1.0';'';'';''};
    
    yyaxis left
    hold on
    fig.dat(itg.fda) = plot(Np.tot(1:n.PLS),(xloads2{j}(indi{j}.mi2,:))+0.5,'k'); itg.fda = itg.fda+1;
    fig.dat(itg.fda) = plot(Np.tot(1:n.PLS),(xloads2{j}(indi{j}.ma2,:))+0.5,'k--'); itg.fda = itg.fda+1;
    fig.label(itg.lab) = ylabel(['${w}_{' num2str(sumpct{end}) ',k}$']); itg.lab = itg.lab+1;
    ylim([-1.25 1.5])    
    fig.axes(1).YTick = -1.0:0.5:1.5;
    fig.axes(1).YTickLabel = {'';'-1.0';'-0.5';'0.0';'0.5';'1.0'};
    fig.label(itg.lab) = xlabel('Number of sampling points $n_p$'); itg.lab = itg.lab+1;
    legend('$\min s^2$','$\max s^2$','Location','east');
    set(fig.axes(itg.axe).Legend,'FontSize',fig.Font(2),'Interpreter','latex','Box','off')
    itg.axe = itg.axe+1;
    itg.fig = itg.fig+1;
end


% set(fig.ann(1:2), 'Interpreter', 'Latex');
% set(fig.ann(1:2), 'Fontsize', 10);
% set(fig.ann(1:2), 'LineStyle', 'none');
set(fig.label, 'Fontsize', fig.Font(1));
set(fig.label, 'Interpreter', 'Latex');
set(fig.axes,'FontSize', fig.Font(2));
% set(fig.axes,'Legend.FontSize', fig.Font(2));
set(fig.axes,'Box','on');
set(fig.axes,'TickLabelInterpreter', 'Latex');
set(fig.axes,'TickLabelInterpreter', 'Latex');
set(fig.axes,'LineWidth',.75);
set(fig.axes,'XMinorTick', 'on');
% set(fig.axes,'YMinorTick', 'on');
set(fig.dat,'LineWidth',.75)
% fig.axes(2).FontSize = 6;
% fig.axes(end).Legend.FontSize = 8;

