%% Header
% Purpose:  Calculation and graphical representation of pareto 
%           optimal PID controller tuning.
% Author:   Axel Lodemel Holene
% E-mail:   axel.holene@gmail.com
% Date:     13. February 2013
% About:    ctrl = [Kc I D]

%% Initialising
clear all
close all
clc

% Adding commonFiles to search path
mDir   = fullfile(...
        '~/Documents/NTNU/Master_thesis/matlab/newWorldOrder/') ;
shared = fullfile(mDir,'commonFiles') ;
m2tkz  = fullfile(mDir,'matlab2tikz/src') ;
addpath(shared)
addpath(m2tkz)

includeSmith = 0 ;

titleName = {'Case 10', 'Case 11', 'Case 12', 'Case 13'} ;
modelName = {'case10', 'case11', 'case12', 'case13'} ;
if includeSmith
    cTypes  = {'po', 'simc', 'smith'} ;
else
    cTypes  = {'po', 'simc'} ;
end
ctrlrs  = {'pid', 'pi'} ;
cParm   = {'Chriss', 'Parallel', 'AltParallel', 'Cascade'} ;

tables = {'PO:PI', 'PO:PID'}

for 









for modelId = 1:1 %length(modelName)
    i = 1 ;                                             % Counter
    J = zeros(80,24) ;
    Ms = J ;
    Mt = J ;
    captions = {};
    for cType = 1:length(cTypes)
        for ctrlr = 1:length(ctrlrs)
            for cNum = 1:length(cParm)
                innerLoop = true ;
                fprintf('Attempting to load %s \t\t', ...
                        strcat( upper(cTypes{cType}), ':', ...
                                upper(ctrlrs{ctrlr}), ':', ...
                                cParm{cNum}))
                try
                    load([mDir, '/results/', ...
                        num2str(cTypes{cType}), '/', ...
                        num2str(ctrlrs{ctrlr}), '/', ...
                        num2str(cParm{cNum}),   '/', ...
                        num2str(modelName{modelId}),'.mat'],...
                        'res')
                catch
                    fprintf('Failed. \n')
                    innerLoop = false ;
                end
                if innerLoop
                    caption = strcat(upper(cTypes{cType}),':', ...
                                    upper(ctrlrs{ctrlr}), ':', ...
                                    cParm{cNum}) ;
                    captions{i} = caption ;
                    this = getfield(res, ...
                                    strcat(upper(cTypes{cType}),...
                                    upper(ctrlrs{ctrlr}))) ;
                    j = this.J ;
                    ms = this.Msc ;
                    mt = this.Mt ;
                    J(1:length(j),i) = j ;
                    Ms(1:length(ms),i) = ms ;
                    Mt(1:length(mt),i) = mt ;
                    fprintf('Success!\n') ;
                end
                i = i + 1 ;
            end
        end
    end
    % Removing zeros
    J(J==0)     = NaN ;
    Ms(Ms == 0) = NaN ;
    Mt(Mt == 0) = NaN ;

    % Plotting routine
    figure(modelId)
    clf
    subplot(2,1,1), plot(Ms, J, 'LineWidth', 1) ;
    axis([0 4 0 4])
    xlabel('Robustness, $M_s$', ...
           'interpreter','latex','FontSize',12)
    ylabel('Performance, $J$', ...
            'interpreter','latex','FontSize',12)
    title(titleName{modelId}, ...
            'interpreter','latex','FontSize',12)
    legend(captions, 'Location','NorthEast')

    subplot(2,1,2), plot(Ms, Mt, 'LineWidth', 1)
    axis([0 3 0 3])
    xlabel('$M_s$', 'interpreter', 'latex', 'FontSize', 12)
    ylabel('$M_t$', 'interpreter', 'latex', 'FontSize', 12)
    title(titleName{modelId},'interpreter','latex','FontSize',12)
    legend(captions, 'Location','NorthEast')

    if includeSmith
        save([mDir, 'results/merged/structs/FO/incSmith/', ...
            num2str(modelName{modelId}),'.mat'])
        saveas(gcf,['results/merged/figures/FO/incSmith',...
           num2str(modelName{modelId}),'.fig'])
        loc = strcat('results/tex/FO/incSmith',...
                    num2str(modelName{modelId}),'.tex');
    else
        save([mDir, 'results/merged/structs/FO/po_simc/', ...
            num2str(modelName{modelId}),'.mat'])
        saveas(gcf,['results/merged/figures/FO/po_simc',...
           num2str(modelName{modelId}),'.fig'])
        loc = strcat('results/tex/FO/po_simc',...
                    num2str(modelName{modelId}),'.tex');
    end
    matlab2tikz(loc, 'width', '\textwidth') ;
end                                             % Ending for-loop
restoredefaultpath