%% Header
% Purpose:  Fixing struct errors. 
% 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
DACO = get(0,'DefaultAxesColorOrder') ;
% Adding commonFiles to search path
mDir = fullfile(pwd) ;
shared = fullfile(mDir, 'commonFiles') ;    addpath(shared)
varclr = fullfile(mDir,'varycolor') ;       addpath(varclr)
m2tkz  = fullfile(mDir,'matlab2tikz/src') ; addpath(m2tkz) 

runTime = cputime ;                % Start total computation time

%% Simulation, process and controller  parameters
modelName = {'case1', 'case2', 'case3', 'case4', 'case5', ...
             'case6', 'case7', 'case8', 'case9', 'case10', ...
             'case11', 'case12', 'case13','case14'} ;
titleName = {'Case 1, $\G_{1}(s)=\gOne$'    ,...
            'Case 2, $\G_{2}(s)=\gTwo$'     ,...
            'Case 3, $\G_{3}(s)=\gThree$'   ,...
            'Case 4, $\G_{4}(s)=\gFour$'    ,...
            'Case 5, $\G_{5}(s)=\gFive$'    ,...
            'Case 6, $\G_{6}(s)=\gSix$'     ,...
            'Case 7, $\G_{7}(s)=\gSev$'     ,...
            'Case 8, $\G_{8}(s)=\gEight$'   ,...
            'Case 9, $\G_{9}(s)=\gNine$'    ,...
            'Case 10, $\G_{10}(s)=\gTen$'   ,...
            'Case 11, $\G_{11}(s)=\gElv$'   ,...
            'Case 12, $\G_{12}(s)=\gTwlv$'  ,...
            'Case 13, $\G_{13}(s)=\gTh$'    , ...
            'Case 14, $\G_{14}(s)=\gFth$'} ;
cTypes  = {'po', 'simc', 'smith', 'smithDist'} ;
cType   = 1 ;
ctrlrs  = {'pid', 'pi', 'p'} ;
ctrlr   = 1 ;
cParm   = {'Chriss', 'Parallel', 'AltParallel', 'Cascade'} ;
cNum    = 1 ;

TODOS = {'fixStructs', 'findRoots', 'newPlots'} ;
TODO = 3 ; 

fprintf('\n')
fprintf('----------------------------------------------------\n')
fprintf('This is %s\n', upper(TODOS{TODO}))
fprintf('%s\n', datestr(clock, 'dd.mm.yy HH:MM'))
fprintf('Main Directory: %s\n', mDir)
fprintf('Target: %s\n', upper(cTypes{cType})) ;
fprintf('Structure: %s\n', upper(ctrlrs{ctrlr})) ;
fprintf('Parametrization: %s\n', cParm{cNum})
fprintf('----------------------------------------------------\n')
for cType = [1 3]
    for ctrlr = [1 2]
        for cNum = 4

work = true ;

if TODO==3
    for modelId = 14:14
        success = true ;
        if ctrlr == 3
            loadstr = strcat('/results/smithDist/', ...
                            cTypes{cType},'/',      ...
                            ctrlrs{ctrlr},'/',      ...
                            modelName{modelId}) ;
        else
            loadstr = strcat('/results/smithDist/', ...
                            cTypes{cType},'/',      ...
                            ctrlrs{ctrlr},'/',      ...
                            cParm{cNum}  ,'/',      ...
                            modelName{modelId}) ;
        end
        fprintf('Attempting to load %s\n', loadstr) ;
        try
            load([mDir, loadstr,'.mat'],'res') ;
        catch
            fprintf('Failed. \n')
            success = false ;
        end
        if success
            if ctrlr == 3
                savestr = strcat('results/smithDist/p/', ...
                                modelName{modelId}) ;
            else
                savestr = strcat('results/smithDist/', ...
                                cTypes{cType},  '/', ...
                                ctrlrs{ctrlr},  '/', ...
                                cParm{cNum},    '/', ...
                                modelName{modelId}) ;
            end % ctrlr == 3
            this = getfield(res,strcat('SMITHDIST',upper(ctrlrs{ctrlr}))) ;
            fprintf('Plotting first time....\n')
            figure(modelId) ; clf
            if length(this.Ms_span)>6 ;
                ColorSet = varycolor(length(this.Ms_span)) ;
                set(0, 'DefaultAxesColorOrder', ColorSet);
            end
            plot(this.d_span,this.J) ;
            legendCell = cellstr(num2str(this.Ms_span, ...
                                            'Ms = %.2f'));
            legend(legendCell, 'Location','EastOutside')
            xlabel('Time-delay error, $\dtheta$', ...
                'interpreter','latex') ;
            ylabel('Performance, $J$','interpreter','latex') ;
            title(titleName{modelId},'interpreter','latex')
            axis([-1 1 0 20])
            loc = strcat(savestr,'.tex') ;
            matlab2tikz(loc,'showInfo', false) ;
            fprintf('Saving first time...\n')
            fprintf('Plotting second time...\n')
            figure(modelId+length(modelName)); clf
            plot(this.d_span,this.Msc) ;
            legend(legendCell, 'Location','EastOutside')
            xlabel('Time-delay error, $\dtheta$', ...
                'interpreter','latex') ;
            ylabel('Robustness, $\Ms$','interpreter','latex') ;
            title(titleName{modelId},'interpreter','latex')
            axis([-1 1 0 20])
            loc = strcat(savestr,'_d_Ms.tex') ;
            matlab2tikz(loc,'showInfo', false) ;
            fprintf('Saving second time...\n')
        end % success
    end % for ModelId
end

errors = struct ;
loadError = struct ;
calcError = struct ;

set(0,'DefaultAxesColorOrder',DACO) ;    % Restoring Color Order

end % cNum
end % ctrlr
end % cType
errors = setfield(errors,'loadError',loadError) ;
errors = setfield(errors,'calcError',calcError) ;
fprintf('\n Calculation finished. Listing errors:\n')
disp(errors)
