clc; clear all;
warning off

load Dati_Matrice_id_chiuso.mat

tic

%intervallo dati di validazione
val_start=1600;
val_stop=2400;

% attenzione in fid ai / o \

%% sseh_norm

fid = fopen('ValidazioneChiuso/sseh_yy_norm_Mnpf.txt', 'w+');

str_spar = 'Sparsit�:\t';
str_num_np = '';
str_nome_dati = '';
str_dati = '';
str_dati_sim = 'Spar_sim\t';

for num_sens=1:length(Dati) % scelta sensore 
    
    yf = Dati(1,num_sens).t.y_norm(val_start:val_stop,:);
    Uf = Dati(1,num_sens).t.Udati_norm(val_start:val_stop,:);
    dat_val = iddata(yf,Uf);
    
    [len_p len_n] = size(Dati(1,num_sens).t.sseh_ny_norm);
    
    str_nome_id = ['\tDati(1,' num2str(num_sens) ').t.sseh_yy_norm(p,n).Mnpf'];
    fprintf(fid, [str_nome_id, '\n']);
    
    disp(['Sensore: Dati(1,' num2str(num_sens) ').t.sseh_yy_norm(p,n).Mnpf']);
    
    for k=1:30 % passo di predizione
        

        str_dati = [str_dati, num2str(k), '\t'];
        
        for num_n=1:len_n
            for num_p=1:len_p
                
                try
                if k==1
                    spars = sparsi(Dati(1,num_sens).t.sseh_yy_norm(num_p,num_n).Mnpf);
                    str_spar = [str_spar num2str(spars) '%%\t\t\t'];
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.sseh_yy_norm(num_p,num_n).n_) ' p=' num2str(Dati(1,num_sens).t.sseh_yy_norm(num_p,num_n).p_) '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                    
                    y_sim = sim(Dati(1,num_sens).t.sseh_yy_norm(num_p,num_n).Mnpf,dat_val.u);
                    y_sim = iddata(y_sim);
                    y_sim = y_sim.y;

                    %Calcolo indici
                    fit = (1 - norm(yf-y_sim)/norm(yf-mean(yf)));
                    rms = sqrt(mean((yf-y_sim).^2));
                    cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));

                    str_dati_sim = [str_dati_sim, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                    
                end
                
                y_pred = predict(Dati(1,num_sens).t.sseh_yy_norm(num_p,num_n).Mnpf,dat_val,k);
                y_pred = iddata(y_pred);
                y_pred = y_pred.y;
                
                %Calcolo indici
                fit = (1 - norm(yf-y_pred)/norm(yf-mean(yf)));
                rms = sqrt(mean((yf-y_pred).^2));
                cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));
                
                
                catch exception
                
                fit = NaN;
                rms = NaN;
                cod = NaN;
                
                if k==1
                    str_spar = [str_spar num2str(NaN) '%%\t\t\t'];
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.sseh_yy_norm(num_p,num_n).n_) ' p=' num2str(Dati(1,num_sens).t.sseh_yy_norm(num_p,num_n).p_) '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                    str_dati_sim = [str_dati_sim, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                end
                                   
                end
                
                str_dati = [str_dati, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                
            end
        end
        str_dati = [str_dati, '\n'];
    end
    
    fprintf(fid, ['\t' str_num_np, '\n']);    
    fprintf(fid, [str_spar '\n']);
    fprintf(fid, ['\t' str_nome_dati, '\n']);
    fprintf(fid, [str_dati]);
    fprintf(fid, [str_dati_sim, '\n \n \n' ]);
    
    str_spar = 'Sparsit�:\t';
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
    str_dati_sim = 'Spar_sim\t';
    
end

fclose(fid);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fid = fopen('ValidazioneChiuso/sseh_ny_norm_Mnpf.txt', 'w+');

str_spar = 'Sparsit�:\t';
str_num_np = '';
str_nome_dati = '';
str_dati = '';
str_dati_sim = 'Spar_sim\t';

for num_sens=1:length(Dati) % scelta sensore 
    
    yf = Dati(1,num_sens).t.y_norm(val_start:val_stop,:);
    Uf = Dati(1,num_sens).t.Udati_norm(val_start:val_stop,:);
    dat_val = iddata(yf,Uf);
    
    [len_p len_n] = size(Dati(1,num_sens).t.sseh_ny_norm);
    
    str_nome_id = ['\tDati(1,' num2str(num_sens) ').t.sseh_ny_norm(p,n).Mnpf'];
    fprintf(fid, [str_nome_id, '\n']);
    
    disp(['Sensore: Dati(1,' num2str(num_sens) ').t.sseh_ny_norm(p,n).Mnpf']);
    
    for k=1:30 % passo di predizione
        

        str_dati = [str_dati, num2str(k), '\t'];
        
        for num_n=1:len_n
            for num_p=1:len_p
                
                try
                if k==1
                    spars = sparsi(Dati(1,num_sens).t.sseh_ny_norm(num_p,num_n).Mnpf);
                    str_spar = [str_spar num2str(spars) '%%\t\t\t'];
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.sseh_ny_norm(num_p,num_n).n_) ' p=' num2str(Dati(1,num_sens).t.sseh_ny_norm(num_p,num_n).p_) '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                    
                    y_sim = sim(Dati(1,num_sens).t.sseh_ny_norm(num_p,num_n).Mnpf,dat_val.u);
                    y_sim = iddata(y_sim);
                    y_sim = y_sim.y;

                    %Calcolo indici
                    fit = (1 - norm(yf-y_sim)/norm(yf-mean(yf)));
                    rms = sqrt(mean((yf-y_sim).^2));
                    cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));

                    str_dati_sim = [str_dati_sim, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                    
                end
                
                y_pred = predict(Dati(1,num_sens).t.sseh_ny_norm(num_p,num_n).Mnpf,dat_val,k);
                y_pred = iddata(y_pred);
                y_pred = y_pred.y;
                
                %Calcolo indici
                fit = (1 - norm(yf-y_pred)/norm(yf-mean(yf)));
                rms = sqrt(mean((yf-y_pred).^2));
                cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));
                
                catch exception
                
                fit = NaN;
                rms = NaN;
                cod = NaN;    
                    
                if k==1
                    str_spar = [str_spar num2str(NaN) '%%\t\t\t'];
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.sseh_ny_norm(num_p,num_n).n_) ' p=' num2str(Dati(1,num_sens).t.sseh_ny_norm(num_p,num_n).p_) '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                    str_dati_sim = [str_dati_sim, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                end
                                   
                end
                
                str_dati = [str_dati, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                
            end
        end
        str_dati = [str_dati, '\n'];
    end
    
    fprintf(fid, ['\t' str_num_np, '\n']);    
    fprintf(fid, [str_spar '\n']);
    fprintf(fid, ['\t' str_nome_dati, '\n']);
    fprintf(fid, [str_dati]);
    fprintf(fid, [str_dati_sim, '\n \n \n' ]);
    
    str_spar = 'Sparsit�:\t';
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
    str_dati_sim = 'Spar_sim\t';
    
end

fclose(fid);


%% ssglar_norm

fid = fopen('ValidazioneChiuso\ssglar_norm_Mnpg.txt', 'w+');

str_spar = 'Sparsit�:\t';
str_num_np = '';
str_nome_dati = '';
str_dati = '';
str_dati_sim = 'Spar_sim\t';

for num_sens=1:length(Dati) % scelta sensore 
    
    yf = Dati(1,num_sens).t.y_norm(val_start:val_stop,:);
    Uf = Dati(1,num_sens).t.Udati_norm(val_start:val_stop,:);
    dat_val = iddata(yf,Uf);
    
    [len_p len_n] = size(Dati(1,num_sens).t.ssglar_norm.mod);
    
    str_nome_id = ['\tDati(1,' num2str(num_sens) ').t.ssglar_norm.mod(p,n).Mnpg'];
    fprintf(fid, [str_nome_id, '\n']);
    
    disp(['Sensore: Dati(1,' num2str(num_sens) ').t.ssglar_norm.mod(p,n).Mnpg']);
    
    for k=1:30 % passo di predizione
        

        str_dati = [str_dati, num2str(k), '\t'];
        
        for num_n=1:len_n
            for num_p=1:len_p
                
                try
                if k==1 
                    spars = sparsi(Dati(1,num_sens).t.ssglar_norm.mod(num_p,num_n).Mnpg);
                    str_spar = [str_spar num2str(spars) '%%\t\t\t'];
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.ssglar_norm.mod(num_p,num_n).n) ' p=' num2str(Dati(1,num_sens).t.ssglar_norm.mod(num_p,num_n).pRKHS) '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                    
                    y_sim = sim(Dati(1,num_sens).t.ssglar_norm.mod(num_p,num_n).Mnpg,dat_val.u);
                    y_sim = iddata(y_sim);
                    y_sim = y_sim.y;

                    %Calcolo indici
                    fit = (1 - norm(yf-y_sim)/norm(yf-mean(yf)));
                    rms = sqrt(mean((yf-y_sim).^2));
                    cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));

                    str_dati_sim = [str_dati_sim, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                    
                end
                
                y_pred = predict(Dati(1,num_sens).t.ssglar_norm.mod(num_p,num_n).Mnpg,dat_val,k);
                y_pred = iddata(y_pred);
                y_pred = y_pred.y;
                
                %Calcolo indici
                fit = (1 - norm(yf-y_pred)/norm(yf-mean(yf)));
                rms = sqrt(mean((yf-y_pred).^2));
                cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));
                
                
                catch exception
                    
                fit = NaN;
                rms = NaN;
                cod = NaN;       
                    
                if k==1 
                    spars = NaN;
                    str_spar = [str_spar num2str(NaN) '%%\t\t\t'];
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.ssglar_norm.mod(num_p,num_n).n) ' p=' num2str(Dati(1,num_sens).t.ssglar_norm.mod(num_p,num_n).pRKHS) '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                    str_dati_sim = [str_dati_sim, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                end
                                
                end
                
                str_dati = [str_dati, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                
            end
        end
        str_dati = [str_dati, '\n'];
    end
    
    fprintf(fid, ['\t' str_num_np, '\n']);    
    fprintf(fid, [str_spar '\n']);
    fprintf(fid, ['\t' str_nome_dati, '\n']);
    fprintf(fid, [str_dati]);
    fprintf(fid, [str_dati_sim, '\n \n \n' ]);
    
    str_spar = 'Sparsit�:\t';
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
    str_dati_sim = 'Spar_sim\t';
    
end

fclose(fid);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fid = fopen('ValidazioneChiuso\ssglar_norm_Mnpc.txt', 'w+');

str_spar = 'Sparsit�:\t';
str_num_np = '';
str_nome_dati = '';
str_dati = '';
str_dati_sim = 'Spar_sim\t';

for num_sens=1:length(Dati) % scelta sensore   
    
    yf = Dati(1,num_sens).t.y_norm(val_start:val_stop,:);
    Uf = Dati(1,num_sens).t.Udati_norm(val_start:val_stop,:);
    dat_val = iddata(yf,Uf);
    
    [len_p len_n] = size(Dati(1,num_sens).t.ssglar_norm.mod);
    
    str_nome_id = ['\tDati(1,' num2str(num_sens) ').t.ssglar_norm.mod(p,n).Mnpc'];
    fprintf(fid, [str_nome_id, '\n']);
    
    disp(['Sensore: Dati(1,' num2str(num_sens) ').t.ssglar_norm.mod(p,n).Mnpc']);

    for k=1:30 % passo di predizione
        

        str_dati = [str_dati, num2str(k), '\t'];
        
        for num_n=1:len_n
            for num_p=1:len_p
                
                try
                if k==1
                    spars = sparsi(Dati(1,num_sens).t.ssglar_norm.mod(num_p,num_n).Mnpc);
                    str_spar = [str_spar num2str(spars) '%%\t\t\t'];
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.ssglar_norm.mod(num_p,num_n).n) ' p=' num2str(Dati(1,num_sens).t.ssglar_norm.mod(num_p,num_n).pRKHS) '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                    
                    y_sim = sim(Dati(1,num_sens).t.ssglar_norm.mod(num_p,num_n).Mnpc,dat_val.u);
                    y_sim = iddata(y_sim);
                    y_sim = y_sim.y;

                    %Calcolo indici
                    fit = (1 - norm(yf-y_sim)/norm(yf-mean(yf)));
                    rms = sqrt(mean((yf-y_sim).^2));
                    cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));

                    str_dati_sim = [str_dati_sim, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                    
                end
                
                y_pred = predict(Dati(1,num_sens).t.ssglar_norm.mod(num_p,num_n).Mnpc,dat_val,k);
                y_pred = iddata(y_pred);
                y_pred = y_pred.y;
                
                %Calcolo indici
                fit = (1 - norm(yf-y_pred)/norm(yf-mean(yf)));
                rms = sqrt(mean((yf-y_pred).^2));
                cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));
                
                catch exception
                    
                fit = NaN;
                rms = NaN;
                cod = NaN;    
                    
                if k==1
                    spars = NaN;
                    str_spar = [str_spar num2str(NaN) '%%\t\t\t'];
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.ssglar_norm.mod(num_p,num_n).n) ' p=' num2str(Dati(1,num_sens).t.ssglar_norm.mod(num_p,num_n).pRKHS) '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                    str_dati_sim = [str_dati_sim, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                end
                                   
                end
                
                str_dati = [str_dati, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                
            end
        end
        str_dati = [str_dati, '\n'];
    end
    
    fprintf(fid, ['\t' str_num_np, '\n']);    
    fprintf(fid, [str_spar '\n']);
    fprintf(fid, ['\t' str_nome_dati, '\n']);
    fprintf(fid, [str_dati]);
    fprintf(fid, [str_dati_sim, '\n \n \n' ]);
    
    str_spar = 'Sparsit�:\t';
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
    str_dati_sim = 'Spar_sim\t';
    
end

fclose(fid);


%% glar_norm

fid = fopen('ValidazioneChiuso\glar_norm_Mgl.txt', 'w+');

str_spar = 'Sparsit�:\t';
str_num_np = '';
str_nome_dati = '';
str_dati = '';
str_dati_sim = 'Spar_sim\t';

for num_sens=1:length(Dati) % scelta sensore 
    
    yf = Dati(1,num_sens).t.y_norm(val_start:val_stop,:);
    Uf = Dati(1,num_sens).t.Udati_norm(val_start:val_stop,:);
    dat_val = iddata(yf,Uf);
    
    [len_p len_n] = size(Dati(1,num_sens).t.glar_norm.mod);
    
    str_nome_id = ['\tDati(1,' num2str(num_sens) ').t.glar_norm.mod(p,n).Mgl'];
    fprintf(fid, [str_nome_id, '\n']);
    
    disp(['Sensore: Dati(1,' num2str(num_sens) ').t.glar_norm.mod(p,n).Mgl']);
    
    for k=1:30 % passo di predizione
        

        str_dati = [str_dati, num2str(k), '\t'];
        
        for num_n=1:len_n
            for num_p=1:len_p
                
                try
                if k==1
                    spars = sparsi(Dati(1,num_sens).t.glar_norm.mod(num_p,num_n).Mgl);
                    str_spar = [str_spar num2str(spars) '%%\t\t\t'];
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.glar_norm.mod(num_p,num_n).n)  '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                    
                    y_sim = sim(Dati(1,num_sens).t.glar_norm.mod(num_p,num_n).Mgl,dat_val.u);
                    y_sim = iddata(y_sim);
                    y_sim = y_sim.y;

                    %Calcolo indici
                    fit = (1 - norm(yf-y_sim)/norm(yf-mean(yf)));
                    rms = sqrt(mean((yf-y_sim).^2));
                    cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));

                    str_dati_sim = [str_dati_sim, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                    
                end
                
                y_pred = predict(Dati(1,num_sens).t.glar_norm.mod(num_p,num_n).Mgl,dat_val,k);
                y_pred = iddata(y_pred);
                y_pred = y_pred.y;
                
                %Calcolo indici
                fit = (1 - norm(yf-y_pred)/norm(yf-mean(yf)));
                rms = sqrt(mean((yf-y_pred).^2));
                cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));
                
                catch exception
                    
                fit = NaN;
                rms = NaN;
                cod = NaN; 
                    
                if k==1
                    str_spar = [str_spar num2str(NaN) '%%\t\t\t'];
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.glar_norm.mod(num_p,num_n).n)  '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                    str_dati_sim = [str_dati_sim, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                end
                                   
                end
                
                str_dati = [str_dati, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                
            end
        end
        str_dati = [str_dati, '\n'];
    end
    
    fprintf(fid, ['\t' str_num_np, '\n']);    
    fprintf(fid, [str_spar '\n']);
    fprintf(fid, ['\t' str_nome_dati, '\n']);
    fprintf(fid, [str_dati]);
    fprintf(fid, [str_dati_sim, '\n \n \n' ]);
    
    str_spar = 'Sparsit�:\t';
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
    str_dati_sim = 'Spar_sim\t';
    
end

fclose(fid);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fid = fopen('ValidazioneChiuso\glar_norm_Mgls.txt', 'w+');

str_spar = 'Sparsit�:\t';
str_num_np = '';
str_nome_dati = '';
str_dati = '';
str_dati_sim = 'Spar_sim\t';

for num_sens=1:length(Dati) % scelta sensore   
    
    yf = Dati(1,num_sens).t.y_norm(val_start:val_stop,:);
    Uf = Dati(1,num_sens).t.Udati_norm(val_start:val_stop,:);
    dat_val = iddata(yf,Uf);
    
    [len_p len_n] = size(Dati(1,num_sens).t.glar_norm.mod);
    
    str_nome_id = ['\tDati(1,' num2str(num_sens) ').t.glar_norm.mod(p,n).Mgls'];
    fprintf(fid, [str_nome_id, '\n']);
    
    disp(['Sensore: Dati(1,' num2str(num_sens) ').t.glar_norm.mod(p,n).Mgls']);

    for k=1:30 % passo di predizione
        

        str_dati = [str_dati, num2str(k), '\t'];
        
        for num_n=1:len_n
            for num_p=1:len_p
                
                try
                if k==1
                    spars = sparsi(Dati(1,num_sens).t.glar_norm.mod(num_p,num_n).Mgls);
                    str_spar = [str_spar num2str(spars) '%%\t\t\t'];
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.glar_norm.mod(num_p,num_n).n) '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                    
                    y_sim = sim(Dati(1,num_sens).t.glar_norm.mod(num_p,num_n).Mgls,dat_val.u);
                    y_sim = iddata(y_sim);
                    y_sim = y_sim.y;

                    %Calcolo indici
                    fit = (1 - norm(yf-y_sim)/norm(yf-mean(yf)));
                    rms = sqrt(mean((yf-y_sim).^2));
                    cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));

                    str_dati_sim = [str_dati_sim, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                    
                end
                
                y_pred = predict(Dati(1,num_sens).t.glar_norm.mod(num_p,num_n).Mgls,dat_val,k);
                y_pred = iddata(y_pred);
                y_pred = y_pred.y;
                
                %Calcolo indici
                fit = (1 - norm(yf-y_pred)/norm(yf-mean(yf)));
                rms = sqrt(mean((yf-y_pred).^2));
                cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));
                
                catch exception
                    
                fit = NaN;
                rms = NaN;
                cod = NaN;     
                    
                if k==1
                    spars = NaN;
                    str_spar = [str_spar num2str(NaN) '%%\t\t\t'];
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.glar_norm.mod(num_p,num_n).n) '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                    str_dati_sim = [str_dati_sim, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                end
                                  
                end
                
                str_dati = [str_dati, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                
            end
        end
        str_dati = [str_dati, '\n'];
    end
    
    fprintf(fid, ['\t' str_num_np, '\n']);    
    fprintf(fid, [str_spar '\n']);
    fprintf(fid, ['\t' str_nome_dati, '\n']);
    fprintf(fid, [str_dati]);
    fprintf(fid, [str_dati_sim, '\n \n \n' ]);
    
    str_spar = 'Sparsit�:\t';
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
    str_dati_sim = 'Spar_sim\t';
    
end

fclose(fid);


%% pem_norm

fid = fopen('ValidazioneChiuso\pem_norm_Mpem_BIC.txt', 'w+');

str_num_np = '';
str_nome_dati = '';
str_dati = '';
str_dati_sim = 'Spar_sim\t';

for num_sens=1:length(Dati) % scelta sensore 
    
    yf = Dati(1,num_sens).t.y_norm(val_start:val_stop,:);
    Uf = Dati(1,num_sens).t.Udati_norm(val_start:val_stop,:);
    dat_val = iddata(yf,Uf);
    
    [len_p len_n] = size(Dati(1,num_sens).t.pem_norm.mod);
    
    str_nome_id = ['\tDati(1,' num2str(num_sens) ').t.pem_norm.mod(p,n).Mpem_BIC'];
    fprintf(fid, [str_nome_id, '\n']);
    
    disp(['Sensore: Dati(1,' num2str(num_sens) ').t.pem_norm.mod(p,n).Mpem_BIC']);
    
    for k=1:30 % passo di predizione
        

        str_dati = [str_dati, num2str(k), '\t'];
        
        for num_n=1:len_n
            for num_p=1:len_p
                
                try
                if k==1
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.pem_norm.mod(num_p,num_n).n)  '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                    
                    y_sim = sim(Dati(1,num_sens).t.pem_norm.mod(num_p,num_n).Mpem_BIC,dat_val.u);
                    y_sim = iddata(y_sim);
                    y_sim = y_sim.y;

                    %Calcolo indici
                    fit = (1 - norm(yf-y_sim)/norm(yf-mean(yf)));
                    rms = sqrt(mean((yf-y_sim).^2));
                    cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));

                    str_dati_sim = [str_dati_sim, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                    
                end
                
                y_pred = predict(Dati(1,num_sens).t.pem_norm.mod(num_p,num_n).Mpem_BIC,dat_val,k);
                y_pred = iddata(y_pred);
                y_pred = y_pred.y;
                
                %Calcolo indici
                fit = (1 - norm(yf-y_pred)/norm(yf-mean(yf)));
                rms = sqrt(mean((yf-y_pred).^2));
                cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));
                
                catch exception
                fit = NaN;
                rms = NaN;
                cod = NaN;
                if k==1
                    str_dati_sim = [str_dati_sim, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                end
                end
                
                str_dati = [str_dati, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                
            end
        end
        str_dati = [str_dati, '\n'];
    end
    
    fprintf(fid, ['\t' str_num_np, '\n\n']);    
    fprintf(fid, ['\t' str_nome_dati, '\n']);
    fprintf(fid, [str_dati]);
    fprintf(fid, [str_dati_sim, '\n \n \n' ]);
    
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
    str_dati_sim = 'Spar_sim\t';
    
end

fclose(fid);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fid = fopen('ValidazioneChiuso\pem_norm_Mpem_AICC.txt', 'w+');

str_num_np = '';
str_nome_dati = '';
str_dati = '';
str_dati_sim = 'Spar_sim\t';

for num_sens=1:length(Dati) % scelta sensore   
    
    yf = Dati(1,num_sens).t.y_norm(val_start:val_stop,:);
    Uf = Dati(1,num_sens).t.Udati_norm(val_start:val_stop,:);
    dat_val = iddata(yf,Uf);
    
    [len_p len_n] = size(Dati(1,num_sens).t.pem_norm.mod);
    
    str_nome_id = ['\tDati(1,' num2str(num_sens) ').t.pem_norm.mod(p,n).Mpem_AICC'];
    fprintf(fid, [str_nome_id, '\n']);
    
    disp(['Sensore: Dati(1,' num2str(num_sens) ').t.pem_norm.mod(p,n).Mpem_AICC']);

    for k=1:30 % passo di predizione
        

        str_dati = [str_dati, num2str(k), '\t'];
        
        for num_n=1:len_n
            for num_p=1:len_p
                
                try
                if k==1
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.pem_norm.mod(num_p,num_n).n) '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                    
                    y_sim = sim(Dati(1,num_sens).t.pem_norm.mod(num_p,num_n).Mpem_AICC,dat_val.u);
                    y_sim = iddata(y_sim);
                    y_sim = y_sim.y;

                    %Calcolo indici
                    fit = (1 - norm(yf-y_sim)/norm(yf-mean(yf)));
                    rms = sqrt(mean((yf-y_sim).^2));
                    cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));

                    str_dati_sim = [str_dati_sim, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                    
                end
                
                y_pred = predict(Dati(1,num_sens).t.pem_norm.mod(num_p,num_n).Mpem_AICC,dat_val,k);
                y_pred = iddata(y_pred);
                y_pred = y_pred.y;
                
                %Calcolo indici
                fit = (1 - norm(yf-y_pred)/norm(yf-mean(yf)));
                rms = sqrt(mean((yf-y_pred).^2));
                cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));
                
                catch exception
                fit = NaN;
                rms = NaN;
                cod = NaN;
                if k==1
                    str_dati_sim = [str_dati_sim, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                end
                end
                
                str_dati = [str_dati, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                
            end
        end
        str_dati = [str_dati, '\n'];
    end
    
    fprintf(fid, ['\t' str_num_np, '\n\n']);    
    fprintf(fid, ['\t' str_nome_dati, '\n']);
    fprintf(fid, [str_dati]);
    fprintf(fid, [str_dati_sim, '\n \n \n' ]);
    
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
    str_dati_sim = 'Spar_sim\t';
    
end

fclose(fid);


%% n4sid_norm

fid = fopen('ValidazioneChiuso\n4sid_norm.txt', 'w+');

str_num_np = '';
str_nome_dati = '';
str_dati = '';
str_dati_sim = 'Spar_sim\t';

for num_sens=1:length(Dati) % scelta sensore 
    
    yf = Dati(1,num_sens).t.y_norm(val_start:val_stop,:);
    Uf = Dati(1,num_sens).t.Udati_norm(val_start:val_stop,:);
    dat_val = iddata(yf,Uf);
    
    [len_p len_n] = size(Dati(1,num_sens).t.n4sid_norm.struc_n4sid);
    
    str_nome_id = ['\tDati(1,' num2str(num_sens) ').t.n4sid_norm.struc_n4sid(1,n).iden_n4sid'];
    fprintf(fid, [str_nome_id, '\n']);
    
    disp(['Sensore: Dati(1,' num2str(num_sens) ').t.n4sid_norm.struc_n4sid(1,n).iden_n4sid']);
    
    for k=1:30 % passo di predizione
        

        str_dati = [str_dati, num2str(k), '\t'];
        
        for num_n=1:len_n
            for num_p=1:len_p
                
                try
                if k==1
                    str_num_np = [str_num_np 'n=' num2str(Dati(1,num_sens).t.n4sid_norm.struc_n4sid(num_p,num_n).n)  '\t\t\t'];
                    str_nome_dati = [str_nome_dati 'fit\trms\tcod\t'];
                    
                    y_sim = sim(Dati(1,num_sens).t.n4sid_norm.struc_n4sid(num_p,num_n).iden_n4sid,dat_val.u);
                    y_sim = iddata(y_sim);
                    y_sim = y_sim.y;

                    %Calcolo indici
                    fit = (1 - norm(yf-y_sim)/norm(yf-mean(yf)));
                    rms = sqrt(mean((yf-y_sim).^2));
                    cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));

                    str_dati_sim = [str_dati_sim, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                    
                end
                
                y_pred = predict(Dati(1,num_sens).t.n4sid_norm.struc_n4sid(num_p,num_n).iden_n4sid,dat_val,k);
                y_pred = iddata(y_pred);
                y_pred = y_pred.y;
                
                %Calcolo indici
                fit = (1 - norm(yf-y_pred)/norm(yf-mean(yf)));
                rms = sqrt(mean((yf-y_pred).^2));
                cod = 1-(rms^2/(mean((yf-mean(yf)).^2)));
                
                catch exception
                fit = NaN;
                rms = NaN;
                cod = NaN;
                if k==1
                    str_dati_sim = [str_dati_sim, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                end
                end
                
                str_dati = [str_dati, num2str(fit) '\t', num2str(rms) '\t', num2str(cod) '\t'];
                
            end
        end
        str_dati = [str_dati, '\n'];
    end
    
    fprintf(fid, ['\t' str_num_np, '\n\n']);    
    fprintf(fid, ['\t' str_nome_dati, '\n']);
    fprintf(fid, [str_dati]);
    fprintf(fid, [str_dati_sim, '\n \n \n' ]);
    
    str_num_np = '';
    str_nome_dati = '';
    str_dati = '';
    str_dati_sim = 'Spar_sim\t';
    
end

fclose(fid);

toc






