function varargout = Moduli_GUI(varargin)

% Begin initialization code - DO NOT EDIT
clc;
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Moduli_GUI_OpeningFcn, ...
                   'gui_OutputFcn',  @Moduli_GUI_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before Moduli_GUI is made visible.
function Moduli_GUI_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for Moduli_GUI
global models;
handles.output = hObject;
set(hObject,'units','normalized','outerposition',[0 .1 .95 .9])
set(handles.datatable,'Data',{});
set(handles.doitpopup,'String',{'save selected models (modelname isfilename)',...
                                'save selected models as ...',...
                                'save all models (modelname isfilename)',...
                                'save all models as ...',...
                                'remove selected models',...
                                'remove all models'});
set(handles.plotsellist,'Max',2);
set(handles.plotalllist,'Max',2);
set(handles.datamodellist,'Max',2);
models = struct('id',{},...
                'name',{},...
                'betamin',{},...
                'betamax',{},...
                'beta',{},...
                'kstart',{},...
                'mustart',{},...
                'Kf',{},...
                'alpha1',{},'alpha3',{},...
                'kappa',{},...
                'c1i',{},'c1c',{},'c2i',{},'c2c',{},'c3i',{},'c3c',{},...
                'nmax',{},...
                'Kun',{},...
                'Kbar',{},...
                'Kiso',{},...
                'Kre',{},...
                'MYun',{},...
                'MYiso',{},...
                'MYre',{},...
                'NUun',{},...
                'NUiso',{},...
                'NUbar',{},...
                'NUre',{},...
                'Aun',{},...
                'Abar',{},...
                'Aiso',{},...
                'unit',{});

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes Moduli_GUI wait for user response (see UIRESUME)
% uiwait(handles.mainfig);


% --- Outputs from this function are returned to the command line.
function varargout = Moduli_GUI_OutputFcn(hObject, eventdata, handles) 
varargout{1} = handles.output; 

function mainfig_CloseRequestFcn(hObject, eventdata, handles)
delete(hObject);
clear all;

function filmsbox_Callback(hObject, eventdata, handles)

value = get(hObject,'Value');
if value==1
    set(handles.filmpanel,'BackgroundColor',[.9 .05 .05]);
    set(handles.falpha1,'Enable','on','FontWeight','bold');
    set(handles.fc1i,'Enable','on','FontWeight','bold');
    set(handles.fc1c,'Enable','on','FontWeight','bold');
    set(handles.filmt1,'BackgroundColor',[.9 .05 .05]);
    set(handles.filmt2,'BackgroundColor',[.9 .05 .05]);
    set(handles.filmt3,'BackgroundColor',[.9 .05 .05]);
    set(handles.filmt4,'BackgroundColor',[.9 .05 .05]);
    set(handles.filmt5,'BackgroundColor',[.9 .05 .05]);
    guidata(hObject, handles);
else
    set(handles.filmpanel,'BackgroundColor',[.831 .816 .784]);
    set(handles.falpha1,'Enable','off','FontWeight','normal');
    set(handles.fc1i,'Enable','off','FontWeight','normal');
    set(handles.fc1c,'Enable','off','FontWeight','normal');
    set(handles.filmt1,'BackgroundColor',[.831 .816 .784]);
    set(handles.filmt2,'BackgroundColor',[.831 .816 .784]);
    set(handles.filmt3,'BackgroundColor',[.831 .816 .784]);
    set(handles.filmt4,'BackgroundColor',[.831 .816 .784]);
    set(handles.filmt5,'BackgroundColor',[.831 .816 .784]);
    guidata(hObject, handles);
end

function tubesbox_Callback(hObject, eventdata, handles)

value = get(hObject,'Value');
if value==1
    set(handles.tubepanel,'BackgroundColor',[.9 .05 .05]);
    set(handles.fkappa,'Enable','on','FontWeight','bold');
    set(handles.fc2i,'Enable','on','FontWeight','bold');
    set(handles.fc2c,'Enable','on','FontWeight','bold');
    set(handles.tubet1,'BackgroundColor',[.9 .05 .05]);
    set(handles.tubet2,'BackgroundColor',[.9 .05 .05]);
    set(handles.tubet3,'BackgroundColor',[.9 .05 .05]);
    set(handles.tubet4,'BackgroundColor',[.9 .05 .05]);
    guidata(hObject, handles);
else
    set(handles.tubepanel,'BackgroundColor',[.831 .816 .784]);
    set(handles.fkappa,'Enable','off','FontWeight','normal');
    set(handles.fc2i,'Enable','off','FontWeight','normal');
    set(handles.fc2c,'Enable','off','FontWeight','normal');
    set(handles.tubet1,'BackgroundColor',[.831 .816 .784]);
    set(handles.tubet2,'BackgroundColor',[.831 .816 .784]);
    set(handles.tubet3,'BackgroundColor',[.831 .816 .784]);
    set(handles.tubet4,'BackgroundColor',[.831 .816 .784]);
    guidata(hObject, handles);
end

function spherebox_Callback(hObject, eventdata, handles)

value = get(hObject,'Value');
if value==1
    set(handles.sphericpanel,'BackgroundColor',[.9 .05 .05]);
    set(handles.falpha3,'Enable','on','FontWeight','bold');
    set(handles.fc3i,'Enable','on','FontWeight','bold');
    set(handles.fc3c,'Enable','on','FontWeight','bold');
    set(handles.spherict1,'BackgroundColor',[.9 .05 .05]);
    set(handles.spherict2,'BackgroundColor',[.9 .05 .05]);
    set(handles.spherict3,'BackgroundColor',[.9 .05 .05]);
    set(handles.spherict4,'BackgroundColor',[.9 .05 .05]);
    set(handles.spherict5,'BackgroundColor',[.9 .05 .05]);
    guidata(hObject, handles);
else
    set(handles.sphericpanel,'BackgroundColor',[.831 .816 .784]);
    set(handles.falpha3,'Enable','off','FontWeight','normal');
    set(handles.fc3i,'Enable','off','FontWeight','normal');
    set(handles.fc3c,'Enable','off','FontWeight','normal');
    set(handles.spherict1,'BackgroundColor',[.831 .816 .784]);
    set(handles.spherict2,'BackgroundColor',[.831 .816 .784]);
    set(handles.spherict3,'BackgroundColor',[.831 .816 .784]);
    set(handles.spherict4,'BackgroundColor',[.831 .816 .784]);
    set(handles.spherict5,'BackgroundColor',[.831 .816 .784]);
    guidata(hObject, handles);
end
function start_Callback(hObject, eventdata, handles)

global models;

ch_k0       = 0;
ch_mu0      = 0;
ch_kf       = 0;
ch_alpha1   = 0;
ch_alpha3   = 0;
ch_beta     = 0;
ch_kappa    = 0;
ch_c11      = 0;
ch_c12      = 0;
ch_c21      = 0;
ch_c22      = 0;
ch_c31      = 0;
ch_c32      = 0;
ch_nmax     = 0;

try
    KMod_0      = str2num(get(handles.fK0,'String'));      
    ch_k0   = 1;
catch exception
    errordlg(['K_0 has to be number!',getReport(exception)]);
end
try
    SMod_0      = str2num(get(handles.fmu0,'String'));   
    ch_mu0  = 1;
catch exception
    errordlg(['MU_0 has to be number!',getReport(exception)]);
end
try
    Kf          = str2num(get(handles.fKf,'String'));            
    ch_kf   = 1;
catch exception
    errordlg(['K_f has to be number!',getReport(exception)]);
end
missmax     = 10^-10;
try 
    alpha1      = str2num(get(handles.falpha1,'String'));
    ch_alpha1 = 1;
catch exception
    errordlg(['alpha1 has to be number!',getReport(exception)]);
end
try
    alpha3      = str2num(get(handles.falpha3,'String'));
    ch_alpha3 = 1;
catch exception
    errordlg(['alpha3 has to be number!',getReport(exception)]);
end
try
    beta        = str2num(get(handles.fbetamin,'String')):.0001:str2num(get(handles.fbetamax,'String'));
    ch_beta = 1;
catch exception
    errordlg(['betamin and betamax have to be numbers!',getReport(exception)]);
end
try
    kappa       = str2num(get(handles.fkappa,'String'));
    ch_kappa = 1;
catch exception
    errordlg(['kappa has to be number!',getReport(exception)]);
end
try 
    c(1,1)  = str2num(get(handles.fc1i,'String'));
    ch_c11 = 1;
catch exception
    errordlg(['isolated fraction for films has to be number!',getReport(exception)]);
end
try 
    c(1,2)      = str2num(get(handles.fc1c,'String'));
    ch_c12 = 1;
catch exception
    errordlg(['connected fraction for films has to be number!',getReport(exception)]);
end
try
    c(2,1)      = str2num(get(handles.fc2i,'String'));
    ch_c21 = 1;
catch exception
    errordlg(['isolated fraction for tubes has to be number!',getReport(exception)]);
end
try
    c(2,2)      = str2num(get(handles.fc2c,'String'));
    ch_c22 = 1;
catch exception
    errordlg(['connected fraction for tubes has to be number!',getReport(exception)]);
end
try
    c(3,1)      = str2num(get(handles.fc3i,'String'));
    ch_c31 = 1;
catch exception
    errordlg(['isolated fraction for sphericals has to be number!',getReport(exception)]);
end
try
    c(3,2)      = str2num(get(handles.fc3c,'String'));
    ch_c32 = 1;
catch exception
    errordlg(['connected fraction for sphericals has to be number!',getReport(exception)]);
end
try
    nmax        = str2num(get(handles.fnmax,'String'));
    ch_nmax = 1;
catch exception
    errordlg(['nmax has to be number!',getReport(exception)]);
end

modelname   = get(handles.fmodname,'String');
datatable   = get(handles.datatable,'Data');
tablesize   = size(datatable);
modsize     = size(models);
unit        = get(handles.funit,'String');

if sum(sum(c))~=1
    errordlg('sum of all isolated and connected fractions has to be 1!');
elseif ch_k0==0 || ...
       ch_mu0==0 || ...
       ch_kf==0 || ...
       ch_alpha1==0 || ...
       ch_alpha3==0 || ...
       ch_beta==0 || ...
       ch_kappa==0 || ...
       ch_c11==0 || ...
       ch_c12==0 || ...
       ch_c21==0 || ...
       ch_c22==0 || ...
       ch_c31==0 || ...
       ch_c32==0 || ...
       ch_nmax==0
    helpdlg('Look for the Values! K0, Mu0, Kf, alpha1, alpha3, beta, kappa .... have to be numbers!')
elseif alpha1==0 && alpha3==0 && kappa==0
    errordlg('alpha1 = alpha3 = kappa = 0 is not possible!');
elseif min(beta)==max(beta)
    errordlg('betamin = betamax is not possible!');
elseif nmax<2
    errordlg('nmax<2 is not possible!'); 
elseif isempty(unit)
    errordlg('please set a unit for bulk and shearmodulus (have to be the same)!'); 
elseif isempty(modelname)
    errordlg('please set a model name!'); 
else
    disp(['Model Name = ',modelname])
    
    Kun     = zeros(1,length(beta));
    MYun    = zeros(1,length(beta));
    Aun     = zeros(1,length(beta));
    NUun    = zeros(1,length(beta));
    
    Kbar    = zeros(1,length(beta));
    Abar    = zeros(1,length(beta));
    NUbar   = zeros(1,length(beta));
    
    Kiso    = zeros(1,length(beta));
    MYiso   = zeros(1,length(beta));
    Aiso    = zeros(1,length(beta));
    NUiso   = zeros(1,length(beta));
    
    Kre     = zeros(1,length(beta));
    MYre    = zeros(1,length(beta));
    NUre    = zeros(1,length(beta));
    
    PHI_temp    = phi(alpha3);
    G_temp      = g(alpha3,PHI_temp);
    wait        = waitbar(0,'Calculating ...');
    set(wait,'Name','Calculating ...');
    for i=1:length(beta)
        %% -------------------------------- %%
        %% UNRELAXED MODULI_GUI             %%
        %% -------------------------------- %%
        %% START INDEX                      %%
        n = 2;
        %% SET INITIAL CONDITIONS           %%
        %% -------------------------------- %%
        %% K                                %%
        K1(1) = KMod_0;
        K1(2) = 0;
        %% MY                               %%
        MY1(1) = SMod_0;
        %% INITIAL ERROR                    %%
        miss1 = 1;
        miss2 = 1;
        %% -------------------------------- %%
        %% RECURSION FOR K_U & MY_U         %%
        %% -------------------------------- %%
        
        while ((miss1>=missmax) && (miss2>=missmax) && (n<=nmax))
            K_temp  = 0;
            MY_temp = 0;
            for j=1:3
                NU_temp     = poisson(K1(n-1),MY1(n-1));
                R_temp      = R(MY1(n-1),K1(n-1));
                B_temp      = B(j,Kf,K1(n-1),NU_temp,kappa,MY1(n-1));
                theta_temp  = theta(j,K1(n-1),NU_temp,alpha1,kappa,G_temp,PHI_temp,R_temp);
                D_temp      = D(j,Kf,K1(1),theta_temp);
                A_temp      = A(j,alpha1,alpha3,K1(n-1),G_temp,R_temp,D_temp,PHI_temp,B_temp,MY1(n-1),NU_temp,kappa,Kf);
                MY_temp     = MY_temp+muunrelaxed(A_temp,c(j,1)*beta(i),c(j,2)*beta(i));
                K_temp      = K_temp+KModulunrelaxed(Kf,K1(1),c(j,1)*beta(i),c(j,2)*beta(i),theta_temp,K1(n-1));
            end
            K1(n)   = 1/(1/K1(1)+K_temp);
            MY1(n)  = 1/(1/MY1(1)+MY_temp);
            %% ERROR FOR MY AND K               %%
            miss1   = abs(K1(n)-K1(n-1));
            miss2   = abs(MY1(n)-MY1(n-1));
            %% -------------------------------- %%
            n       = n + 1;
            
        end
        %% -------------------------------- %%
        %% disp(['K_u calculated for beta = ',num2str(beta(i)),' ! -> error @ iteration ',num2str(n-1),' = ',num2str(miss1)]);
        %% SET VALUES FOR UNRELAXED TERMS   %%
        %% -------------------------------- %%
        Kun(i)  = K1(n-1);
        MYun(i) = MY1(n-1);
        Aun(i)  = A_temp;
        NUun(i) = NU_temp;
        %% -------------------------------- %%
        %% -------------------------------- %%
        %% -------------------------------- %%
        %% KMODULI' and RELAXED SHEARMODULI %%
        %% -------------------------------- %%
        %% START INDEX                      %%
        n       = 2;
        %% SET INITIAL CONDITIONS           %%
        %% -------------------------------- %%
        %% K'                               %%
        K2(1)   = KMod_0;
        K2(2)   = 0;
        %% MY_R                             %%
        MY2(1)  = SMod_0;
        %% INITIAL ERROR                    %%
        miss1 = 1;
        miss2 = 1;
        %% -------------------------------- %%
        %% RECURSION FOR K' & MY_R          %%
        %% -------------------------------- %%
        while (miss1>=missmax && miss2>=missmax && n<=nmax)
            K_temp  = 0;
            MY_temp = 0;
            for j=1:3
                NU_temp     = poisson(K2(n-1),MY2(n-1));
                R_temp      = R(MY2(n-1),K2(n-1));
                B1_temp     = B(j,Kf,K2(n-1),NU_temp,kappa,MY2(n-1));
                B2_temp     = B(j,0,K2(n-1),NU_temp,kappa,MY2(n-1));
                theta_temp  = theta(j,K2(n-1),NU_temp,alpha1,kappa,G_temp,PHI_temp,R_temp);
                D1_temp     = D(j,Kf,K2(1),theta_temp);
                D2_temp     = D(j,0,K2(1),theta_temp);
                A1_temp     = A(j,alpha1,alpha3,K2(n-1),G_temp,R_temp,D1_temp,PHI_temp,B1_temp,MY2(n-1),NU_temp,kappa,Kf);
                A2_temp     = A(j,alpha1,alpha3,K2(n-1),G_temp,R_temp,D2_temp,PHI_temp,B2_temp,MY2(n-1),NU_temp,kappa,0);
                MY_temp     = MY_temp+murelaxed(A1_temp,A2_temp,c(j,1)*beta(i),c(j,2)*beta(i));
                K_temp      = K_temp+KModulbar(Kf,K2(1),theta_temp,K2(n-1),c(j,1)*beta(i),c(j,2)*beta(i));
            end
            K2(n)   = 1/(1/K2(1)+K_temp);
            MY2(n)  = 1/(1/MY2(1)+MY_temp);
            %% ERROR FOR MY_R AND K'            %%
            miss1   = abs(K2(n)-K2(n-1));
            miss2   = abs(MY2(n)-MY2(n-1));
            %% -------------------------------- %%
            n       = n + 1;
        end
        %% -------------------------------- %%
        %% disp(['K'' calculated for beta = ',num2str(beta(i)),' ! -> error @ iteration ',num2str(n-1),' = ',num2str(miss1)]);
        %% SET VALUES FOR BAR AND MY_RELAXED TERMS %%
        %% --------------------------------------- %%
        MYre(i)     = MY2(n-1);
        Kbar(i)     = K2(n-1);
        Abar(i)     = A1_temp;
        NUbar(i)    = NU_temp;
        %% --------------------------------------- %%
        %% --------------------------------------- %%
        %% --------------------------------------- %%
        %% KMODULI ISOLATED                        %%
        %% --------------------------------------- %%
        %% START INDEX                             %%
        n = 2;
        %% SET INITIAL CONDITIONS                  %%
        %% --------------------------------------- %%
        %% K'                                      %%
        K3(1)   = KMod_0;
        K3(2)   = 0;
        %% MY_R                                    %%
        MY3(1)  = SMod_0;
        %% INITIAL ERROR                           %%
        miss1   = 1;
        miss2   = 1;
        %% --------------------------------------- %%
        %% RECURSION FOR K_ISO & MY_ISO            %%
        %% --------------------------------------- %%
        while (miss1 >= missmax && miss2 >= missmax && n <= nmax)
            K_temp  = 0;
            MY_temp = 0;
            for j=1:3
                betamod     = (c(j,1)*beta(i))/(1-c(j,2)*beta(i));
                NU_temp     = poisson(K3(n-1),MY3(n-1));
                R_temp      = R(MY3(n-1),K3(n-1));
                B_temp      = B(j,Kf,K3(n-1),NU_temp,kappa,MY3(n-1));
                theta_temp  = theta(j,K3(n-1),NU_temp,alpha1,kappa,G_temp,PHI_temp,R_temp);
                D_temp      = D(j,Kf,K3(1),theta_temp);
                A_temp      = A(j,alpha1,alpha3,K3(n-1),G_temp,R_temp,D_temp,PHI_temp,B_temp,MY3(n-1),NU_temp,kappa,Kf);
                MY_temp     = MY_temp+muiso(A_temp,betamod);
                K_temp      = K_temp+KModuliso(Kf,K3(1),betamod,theta_temp,K3(n-1));
            end
            K3(n)   = 1/(1/K3(1)+K_temp);
            MY3(n)  = 1/(1/MY3(1)+MY_temp);
            %% ERROR FOR MY_ISO AND K_ISO              %%
            miss1 = abs(K3(n)-K3(n-1));
            miss2 = abs(MY3(n)-MY3(n-1));
            %% --------------------------------------- %%
            n = n + 1;
        end
        %% --------------------------------------- %%
        %% disp(['K_iso calculated for betamod = ',num2str(beta(i)),' ! -> error @ iteration ',num2str(n-1),' = ',num2str(miss1)]);
        %% SET VALUES FOR ISOLATED TERMS           %%
        %% --------------------------------------- %%
        Kiso(i)     = K3(n-1);
        MYiso(i)    = MY3(n-1);
        Aiso(i)     = A_temp;
        NUiso(i)    = NU_temp;
        %% --------------------------------------- %%
        %% SET VALUES FOR RELAXED TERMS            %%
        %% --------------------------------------- %%
        Kre(i)      = KModulrelaxed(Kf,Kiso(i),Kbar(i),(c(1,2)+c(2,2)+c(3,2))*beta(i));
        NUre(i)     = poisson(Kre(i),MYre(i));
        %% --------------------------------------- %%
        waitbar(i/length(beta),wait,['beta=',sprintf('%1.6f',beta(i))]);
    end
    close(wait);
    m_ind = tablesize(1)+1;
    mod_ind = modsize(2)+1;
    
    datatable{m_ind,1} = modelname;
    datatable{m_ind,2} = min(beta);
    datatable{m_ind,3} = max(beta);
    datatable{m_ind,4} = KMod_0;
    datatable{m_ind,5} = SMod_0;
    datatable{m_ind,6} = Kf;
    datatable{m_ind,7} = alpha1;
    datatable{m_ind,8} = alpha3;
    datatable{m_ind,9} = kappa;
    datatable{m_ind,10} = c(1,1);
    datatable{m_ind,11} = c(1,2);
    datatable{m_ind,12} = c(2,1);
    datatable{m_ind,13} = c(2,2);
    datatable{m_ind,14} = c(3,1);
    datatable{m_ind,15} = c(3,2);
    datatable{m_ind,16} = nmax;
    set(handles.datatable,'Data',datatable);
    
    for i=1:m_ind
        modnames{i} = datatable{i,1};
    end
    
    set(handles.datamodellist,'String',modnames);
    set(handles.plotalllist,'String',modnames);
    
    models(mod_ind).id = mod_ind;
    models(mod_ind).name = modelname;
    models(mod_ind).betamin = min(beta);
    models(mod_ind).betamax = max(beta);
    models(mod_ind).beta = beta;
    models(mod_ind).kstart = KMod_0;
    models(mod_ind).mustart = SMod_0;
    models(mod_ind).Kf = Kf;
    models(mod_ind).alpha1 = alpha1;
    models(mod_ind).alpha3 = alpha3;
    models(mod_ind).kappa = kappa;
    models(mod_ind).c1i = c(1,1);
    models(mod_ind).c1c = c(1,2);
    models(mod_ind).c2i = c(2,1);
    models(mod_ind).c2c = c(2,2);
    models(mod_ind).c3i = c(3,1);
    models(mod_ind).c3c = c(3,2);
    models(mod_ind).nmax = nmax;
    models(mod_ind).Kun = Kun;
    models(mod_ind).Kbar = Kbar;
    models(mod_ind).Kiso = Kiso;
    models(mod_ind).Kre = Kre;
    models(mod_ind).MYun = MYun;
    models(mod_ind).MYiso = MYiso;
    models(mod_ind).MYre = MYre;
    models(mod_ind).NUun = NUun;
    models(mod_ind).NUiso = NUiso;
    models(mod_ind).NUbar = NUbar;
    models(mod_ind).NUre = NUre;
    models(mod_ind).Aun = Aun;
    models(mod_ind).Abar = Abar;
    models(mod_ind).Aiso = Aiso;
    models(mod_ind).unit = unit;
    
    set(handles.fmodname,'String',['Model',num2str(mod_ind+1)]);
    
    guidata(hObject, handles);
end
function doitbutton_Callback(hObject, eventdata, handles)

global models;

modsize = size(models);
doval   = get(handles.doitpopup,'Value');
modval  = get(handles.datamodellist,'Value');
switch doval
    case 1
        wait        = waitbar(0,'Saving ...');
        set(wait,'Name','Saving ...');
        for i=1:length(modval)
            savemodel   = models(modval(i));
            savemodel   = struct2cell(savemodel);
            disp([savemodel{2},'.txt']);
            fid = fopen([savemodel{2},'.txt'], 'w');
            
            %% FIRST LINE %%
            fprintf(fid,'%8s ','beta');
            fprintf(fid,'%8s ','Kun');
            fprintf(fid,'%8s ','Kbar');
            fprintf(fid,'%8s ','Kiso');
            fprintf(fid,'%8s ','Kre');
            fprintf(fid,'%8s ','MUun');
            fprintf(fid,'%8s ','MUiso');
            fprintf(fid,'%8s ','MUre');
            fprintf(fid,'%8s ','NUun');
            fprintf(fid,'%8s ','NUiso');
            fprintf(fid,'%8s ','NUbar');
            fprintf(fid,'%8s ','NUre');
            fprintf(fid,'%8s ','Aun');
            fprintf(fid,'%8s ','Abar');
            fprintf(fid,'%8s ','Aiso');
            fprintf(fid,'%3s ','id');
            fprintf(fid,'%20s ','modelname');
            fprintf(fid,'%8s ','betamin');
            fprintf(fid,'%8s ','betamax');
            fprintf(fid,'%8s ','K0');
            fprintf(fid,'%8s ','MU0');
            fprintf(fid,'%8s ','Kf');
            fprintf(fid,'%8s ','alpha1');
            fprintf(fid,'%8s ','alpha3');
            fprintf(fid,'%8s ','kappa');
            fprintf(fid,'%8s ','c1i');
            fprintf(fid,'%8s ','c1c');
            fprintf(fid,'%8s ','c2i');
            fprintf(fid,'%8s ','c2c');
            fprintf(fid,'%8s ','c3i');
            fprintf(fid,'%8s ','c3c');
            fprintf(fid,'%13s ','nmax');
            fprintf(fid,'%20s\n','unit');
            %% -------------------------------- %%
            %% SECOND LINE %%
            fprintf(fid,'%2.6f ',savemodel{5}(1));
            fprintf(fid,'%2.6f ',savemodel{19}(1));
            fprintf(fid,'%2.6f ',savemodel{20}(1));
            fprintf(fid,'%2.6f ',savemodel{21}(1));
            fprintf(fid,'%2.6f ',savemodel{22}(1));
            fprintf(fid,'%2.6f ',savemodel{23}(1));
            fprintf(fid,'%2.6f ',savemodel{24}(1));
            fprintf(fid,'%2.6f ',savemodel{25}(1));
            fprintf(fid,'%2.6f ',savemodel{26}(1));
            fprintf(fid,'%2.6f ',savemodel{27}(1));
            fprintf(fid,'%2.6f ',savemodel{28}(1));
            fprintf(fid,'%2.6f ',savemodel{29}(1));
            fprintf(fid,'%2.6f ',savemodel{30}(1));
            fprintf(fid,'%2.6f ',savemodel{31}(1));
            fprintf(fid,'%2.6f ',savemodel{32}(1));
            fprintf(fid,'%3d ',savemodel{1});
            fprintf(fid,'%20s ',savemodel{2});
            fprintf(fid,'%2.6f ',savemodel{3});
            fprintf(fid,'%2.6f ',savemodel{4});
            fprintf(fid,'%2.6f ',savemodel{6});
            fprintf(fid,'%2.6f ',savemodel{7});
            fprintf(fid,'%2.6f ',savemodel{8});
            fprintf(fid,'%2.6f ',savemodel{9});
            fprintf(fid,'%2.6f ',savemodel{10});
            fprintf(fid,'%2.6f ',savemodel{11});
            fprintf(fid,'%2.6f ',savemodel{12});
            fprintf(fid,'%2.6f ',savemodel{13});
            fprintf(fid,'%2.6f ',savemodel{14});
            fprintf(fid,'%2.6f ',savemodel{15});
            fprintf(fid,'%2.6f ',savemodel{16});
            fprintf(fid,'%2.6f ',savemodel{17});
            fprintf(fid,'%e ',savemodel{18});
            fprintf(fid,'%20s\n',savemodel{33});
            %% -------------------------------- %%
            %% REST %%
            for k=2:length(savemodel{5})
                fprintf(fid,'%2.6f ',savemodel{5}(k));
                fprintf(fid,'%2.6f ',savemodel{19}(k));
                fprintf(fid,'%2.6f ',savemodel{20}(k));
                fprintf(fid,'%2.6f ',savemodel{21}(k));
                fprintf(fid,'%2.6f ',savemodel{22}(k));
                fprintf(fid,'%2.6f ',savemodel{23}(k));
                fprintf(fid,'%2.6f ',savemodel{24}(k));
                fprintf(fid,'%2.6f ',savemodel{25}(k));
                fprintf(fid,'%2.6f ',savemodel{26}(k));
                fprintf(fid,'%2.6f ',savemodel{27}(k));
                fprintf(fid,'%2.6f ',savemodel{28}(k));
                fprintf(fid,'%2.6f ',savemodel{29}(k));
                fprintf(fid,'%2.6f ',savemodel{30}(k));
                fprintf(fid,'%2.6f ',savemodel{31}(k));
                fprintf(fid,'%2.6f\n',savemodel{32}(k));
            end
            fclose(fid);
            waitbar(i/modsize(2),wait,['model: ',sprintf('%20s',savemodel{2})]);
        end
        close(wait);
    case 2
        wait        = waitbar(0,'Saving ...');
        set(wait,'Name','Saving ...');
        for i=1:length(modval)
            savemodel   = models(modval(i));
            savemodel   = struct2cell(savemodel);
            [savename,savepath]  = uiputfile('*.txt','Save as');
            
            if isempty(savename) || ...
               isempty(savepath)
                disp('NO SAVING!USER ABORT!')
                waitbar(i/length(modval),wait,['not saving ... ',sprintf('%20s',savemodel{2})]);
            else
                fid = fopen([savepath,savename], 'w');
                
                %% FIRST LINE %%
                fprintf(fid,'%8s ','beta');
                fprintf(fid,'%8s ','Kun');
                fprintf(fid,'%8s ','Kbar');
                fprintf(fid,'%8s ','Kiso');
                fprintf(fid,'%8s ','Kre');
                fprintf(fid,'%8s ','MUun');
                fprintf(fid,'%8s ','MUiso');
                fprintf(fid,'%8s ','MUre');
                fprintf(fid,'%8s ','NUun');
                fprintf(fid,'%8s ','NUiso');
                fprintf(fid,'%8s ','NUbar');
                fprintf(fid,'%8s ','NUre');
                fprintf(fid,'%8s ','Aun');
                fprintf(fid,'%8s ','Abar');
                fprintf(fid,'%8s ','Aiso');
                fprintf(fid,'%3s ','id');
                fprintf(fid,'%20s ','modelname');
                fprintf(fid,'%8s ','betamin');
                fprintf(fid,'%8s ','betamax');
                fprintf(fid,'%8s ','K0');
                fprintf(fid,'%8s ','MU0');
                fprintf(fid,'%8s ','Kf');
                fprintf(fid,'%8s ','alpha1');
                fprintf(fid,'%8s ','alpha3');
                fprintf(fid,'%8s ','kappa');
                fprintf(fid,'%8s ','c1i');
                fprintf(fid,'%8s ','c1c');
                fprintf(fid,'%8s ','c2i');
                fprintf(fid,'%8s ','c2c');
                fprintf(fid,'%8s ','c3i');
                fprintf(fid,'%8s ','c3c');
                fprintf(fid,'%13s ','nmax');
                fprintf(fid,'%20s\n','unit');
                %% -------------------------------- %%
                %% SECOND LINE %%
                fprintf(fid,'%2.6f ',savemodel{5}(1));
                fprintf(fid,'%2.6f ',savemodel{19}(1));
                fprintf(fid,'%2.6f ',savemodel{20}(1));
                fprintf(fid,'%2.6f ',savemodel{21}(1));
                fprintf(fid,'%2.6f ',savemodel{22}(1));
                fprintf(fid,'%2.6f ',savemodel{23}(1));
                fprintf(fid,'%2.6f ',savemodel{24}(1));
                fprintf(fid,'%2.6f ',savemodel{25}(1));
                fprintf(fid,'%2.6f ',savemodel{26}(1));
                fprintf(fid,'%2.6f ',savemodel{27}(1));
                fprintf(fid,'%2.6f ',savemodel{28}(1));
                fprintf(fid,'%2.6f ',savemodel{29}(1));
                fprintf(fid,'%2.6f ',savemodel{30}(1));
                fprintf(fid,'%2.6f ',savemodel{31}(1));
                fprintf(fid,'%2.6f ',savemodel{32}(1));
                fprintf(fid,'%3d ',savemodel{1});
                fprintf(fid,'%20s ',savemodel{2});
                fprintf(fid,'%2.6f ',savemodel{3});
                fprintf(fid,'%2.6f ',savemodel{4});
                fprintf(fid,'%2.6f ',savemodel{6});
                fprintf(fid,'%2.6f ',savemodel{7});
                fprintf(fid,'%2.6f ',savemodel{8});
                fprintf(fid,'%2.6f ',savemodel{9});
                fprintf(fid,'%2.6f ',savemodel{10});
                fprintf(fid,'%2.6f ',savemodel{11});
                fprintf(fid,'%2.6f ',savemodel{12});
                fprintf(fid,'%2.6f ',savemodel{13});
                fprintf(fid,'%2.6f ',savemodel{14});
                fprintf(fid,'%2.6f ',savemodel{15});
                fprintf(fid,'%2.6f ',savemodel{16});
                fprintf(fid,'%2.6f ',savemodel{17});
                fprintf(fid,'%e ',savemodel{18});
                fprintf(fid,'%20s\n',savemodel{33});
                %% -------------------------------- %%
                %% REST %%
                for k=2:length(savemodel{5})
                    fprintf(fid,'%2.6f ',savemodel{5}(k));
                    fprintf(fid,'%2.6f ',savemodel{19}(k));
                    fprintf(fid,'%2.6f ',savemodel{20}(k));
                    fprintf(fid,'%2.6f ',savemodel{21}(k));
                    fprintf(fid,'%2.6f ',savemodel{22}(k));
                    fprintf(fid,'%2.6f ',savemodel{23}(k));
                    fprintf(fid,'%2.6f ',savemodel{24}(k));
                    fprintf(fid,'%2.6f ',savemodel{25}(k));
                    fprintf(fid,'%2.6f ',savemodel{26}(k));
                    fprintf(fid,'%2.6f ',savemodel{27}(k));
                    fprintf(fid,'%2.6f ',savemodel{28}(k));
                    fprintf(fid,'%2.6f ',savemodel{29}(k));
                    fprintf(fid,'%2.6f ',savemodel{30}(k));
                    fprintf(fid,'%2.6f ',savemodel{31}(k));
                    fprintf(fid,'%2.6f\n',savemodel{32}(k));
                end
                fclose(fid);
                waitbar(i/length(modval),wait,['saving ... ',sprintf('%20s',savemodel{2})]);
            end
        end
        close(wait);
    case 3
        wait        = waitbar(0,'Saving ...');
        set(wait,'Name','Saving ...');
        for i=1:modsize(2)
            savemodel   = models(i);
            savemodel   = struct2cell(savemodel);
            disp([savemodel{2},'.txt']);
            fid = fopen([savemodel{2},'.txt'], 'w');
            
            %% FIRST LINE %%
            fprintf(fid,'%8s ','beta');
            fprintf(fid,'%8s ','Kun');
            fprintf(fid,'%8s ','Kbar');
            fprintf(fid,'%8s ','Kiso');
            fprintf(fid,'%8s ','Kre');
            fprintf(fid,'%8s ','MUun');
            fprintf(fid,'%8s ','MUiso');
            fprintf(fid,'%8s ','MUre');
            fprintf(fid,'%8s ','NUun');
            fprintf(fid,'%8s ','NUiso');
            fprintf(fid,'%8s ','NUbar');
            fprintf(fid,'%8s ','NUre');
            fprintf(fid,'%8s ','Aun');
            fprintf(fid,'%8s ','Abar');
            fprintf(fid,'%8s ','Aiso');
            fprintf(fid,'%3s ','id');
            fprintf(fid,'%20s ','modelname');
            fprintf(fid,'%8s ','betamin');
            fprintf(fid,'%8s ','betamax');
            fprintf(fid,'%8s ','K0');
            fprintf(fid,'%8s ','MU0');
            fprintf(fid,'%8s ','Kf');
            fprintf(fid,'%8s ','alpha1');
            fprintf(fid,'%8s ','alpha3');
            fprintf(fid,'%8s ','kappa');
            fprintf(fid,'%8s ','c1i');
            fprintf(fid,'%8s ','c1c');
            fprintf(fid,'%8s ','c2i');
            fprintf(fid,'%8s ','c2c');
            fprintf(fid,'%8s ','c3i');
            fprintf(fid,'%8s ','c3c');
            fprintf(fid,'%13s ','nmax');
            fprintf(fid,'%20s\n','unit');
            %% -------------------------------- %%
            %% SECOND LINE %%
            fprintf(fid,'%2.6f ',savemodel{5}(1));
            fprintf(fid,'%2.6f ',savemodel{19}(1));
            fprintf(fid,'%2.6f ',savemodel{20}(1));
            fprintf(fid,'%2.6f ',savemodel{21}(1));
            fprintf(fid,'%2.6f ',savemodel{22}(1));
            fprintf(fid,'%2.6f ',savemodel{23}(1));
            fprintf(fid,'%2.6f ',savemodel{24}(1));
            fprintf(fid,'%2.6f ',savemodel{25}(1));
            fprintf(fid,'%2.6f ',savemodel{26}(1));
            fprintf(fid,'%2.6f ',savemodel{27}(1));
            fprintf(fid,'%2.6f ',savemodel{28}(1));
            fprintf(fid,'%2.6f ',savemodel{29}(1));
            fprintf(fid,'%2.6f ',savemodel{30}(1));
            fprintf(fid,'%2.6f ',savemodel{31}(1));
            fprintf(fid,'%2.6f ',savemodel{32}(1));
            fprintf(fid,'%3d ',savemodel{1});
            fprintf(fid,'%20s ',savemodel{2});
            fprintf(fid,'%2.6f ',savemodel{3});
            fprintf(fid,'%2.6f ',savemodel{4});
            fprintf(fid,'%2.6f ',savemodel{6});
            fprintf(fid,'%2.6f ',savemodel{7});
            fprintf(fid,'%2.6f ',savemodel{8});
            fprintf(fid,'%2.6f ',savemodel{9});
            fprintf(fid,'%2.6f ',savemodel{10});
            fprintf(fid,'%2.6f ',savemodel{11});
            fprintf(fid,'%2.6f ',savemodel{12});
            fprintf(fid,'%2.6f ',savemodel{13});
            fprintf(fid,'%2.6f ',savemodel{14});
            fprintf(fid,'%2.6f ',savemodel{15});
            fprintf(fid,'%2.6f ',savemodel{16});
            fprintf(fid,'%2.6f ',savemodel{17});
            fprintf(fid,'%e ',savemodel{18});
            fprintf(fid,'%20s\n',savemodel{33});
            %% -------------------------------- %%
            %% REST %%
            for k=2:length(savemodel{5})
                fprintf(fid,'%2.6f ',savemodel{5}(k));
                fprintf(fid,'%2.6f ',savemodel{19}(k));
                fprintf(fid,'%2.6f ',savemodel{20}(k));
                fprintf(fid,'%2.6f ',savemodel{21}(k));
                fprintf(fid,'%2.6f ',savemodel{22}(k));
                fprintf(fid,'%2.6f ',savemodel{23}(k));
                fprintf(fid,'%2.6f ',savemodel{24}(k));
                fprintf(fid,'%2.6f ',savemodel{25}(k));
                fprintf(fid,'%2.6f ',savemodel{26}(k));
                fprintf(fid,'%2.6f ',savemodel{27}(k));
                fprintf(fid,'%2.6f ',savemodel{28}(k));
                fprintf(fid,'%2.6f ',savemodel{29}(k));
                fprintf(fid,'%2.6f ',savemodel{30}(k));
                fprintf(fid,'%2.6f ',savemodel{31}(k));
                fprintf(fid,'%2.6f\n',savemodel{32}(k));
            end
            fclose(fid);
            waitbar(i/modsize(2),wait,['saving ... ',sprintf('%20s',savemodel{2})]);
        end
        close(wait)
    case 4
        wait        = waitbar(0,'Saving ...');
        set(wait,'Name','Saving ...');
        for i=1:modsize(2)
            savemodel   = models(i);
            savemodel   = struct2cell(savemodel);
            [savename,savepath]  = uiputfile('*.txt','Save as');
            if isempty(savename) || ...
               isempty(savepath)
                disp('NO SAVING!USER ABORT!')
                waitbar(i/length(modval),wait,['not saving ... ',sprintf('%20s',savemodel{2})]);
            else
                fid = fopen([savepath,savename], 'w');
                
                %% FIRST LINE %%
                fprintf(fid,'%8s ','beta');
                fprintf(fid,'%8s ','Kun');
                fprintf(fid,'%8s ','Kbar');
                fprintf(fid,'%8s ','Kiso');
                fprintf(fid,'%8s ','Kre');
                fprintf(fid,'%8s ','MUun');
                fprintf(fid,'%8s ','MUiso');
                fprintf(fid,'%8s ','MUre');
                fprintf(fid,'%8s ','NUun');
                fprintf(fid,'%8s ','NUiso');
                fprintf(fid,'%8s ','NUbar');
                fprintf(fid,'%8s ','NUre');
                fprintf(fid,'%8s ','Aun');
                fprintf(fid,'%8s ','Abar');
                fprintf(fid,'%8s ','Aiso');
                fprintf(fid,'%3s ','id');
                fprintf(fid,'%20s ','modelname');
                fprintf(fid,'%8s ','betamin');
                fprintf(fid,'%8s ','betamax');
                fprintf(fid,'%8s ','K0');
                fprintf(fid,'%8s ','MU0');
                fprintf(fid,'%8s ','Kf');
                fprintf(fid,'%8s ','alpha1');
                fprintf(fid,'%8s ','alpha3');
                fprintf(fid,'%8s ','kappa');
                fprintf(fid,'%8s ','c1i');
                fprintf(fid,'%8s ','c1c');
                fprintf(fid,'%8s ','c2i');
                fprintf(fid,'%8s ','c2c');
                fprintf(fid,'%8s ','c3i');
                fprintf(fid,'%8s ','c3c');
                fprintf(fid,'%13s ','nmax');
                fprintf(fid,'%20s\n','unit');
                %% -------------------------------- %%
                %% SECOND LINE %%
                fprintf(fid,'%2.6f ',savemodel{5}(1));
                fprintf(fid,'%2.6f ',savemodel{19}(1));
                fprintf(fid,'%2.6f ',savemodel{20}(1));
                fprintf(fid,'%2.6f ',savemodel{21}(1));
                fprintf(fid,'%2.6f ',savemodel{22}(1));
                fprintf(fid,'%2.6f ',savemodel{23}(1));
                fprintf(fid,'%2.6f ',savemodel{24}(1));
                fprintf(fid,'%2.6f ',savemodel{25}(1));
                fprintf(fid,'%2.6f ',savemodel{26}(1));
                fprintf(fid,'%2.6f ',savemodel{27}(1));
                fprintf(fid,'%2.6f ',savemodel{28}(1));
                fprintf(fid,'%2.6f ',savemodel{29}(1));
                fprintf(fid,'%2.6f ',savemodel{30}(1));
                fprintf(fid,'%2.6f ',savemodel{31}(1));
                fprintf(fid,'%2.6f ',savemodel{32}(1));
                fprintf(fid,'%3d ',savemodel{1});
                fprintf(fid,'%20s ',savemodel{2});
                fprintf(fid,'%2.6f ',savemodel{3});
                fprintf(fid,'%2.6f ',savemodel{4});
                fprintf(fid,'%2.6f ',savemodel{6});
                fprintf(fid,'%2.6f ',savemodel{7});
                fprintf(fid,'%2.6f ',savemodel{8});
                fprintf(fid,'%2.6f ',savemodel{9});
                fprintf(fid,'%2.6f ',savemodel{10});
                fprintf(fid,'%2.6f ',savemodel{11});
                fprintf(fid,'%2.6f ',savemodel{12});
                fprintf(fid,'%2.6f ',savemodel{13});
                fprintf(fid,'%2.6f ',savemodel{14});
                fprintf(fid,'%2.6f ',savemodel{15});
                fprintf(fid,'%2.6f ',savemodel{16});
                fprintf(fid,'%2.6f ',savemodel{17});
                fprintf(fid,'%e ',savemodel{18});
                fprintf(fid,'%20s\n',savemodel{33});
                %% -------------------------------- %%
                %% REST %%
                for k=2:length(savemodel{5})
                    fprintf(fid,'%2.6f ',savemodel{5}(k));
                    fprintf(fid,'%2.6f ',savemodel{19}(k));
                    fprintf(fid,'%2.6f ',savemodel{20}(k));
                    fprintf(fid,'%2.6f ',savemodel{21}(k));
                    fprintf(fid,'%2.6f ',savemodel{22}(k));
                    fprintf(fid,'%2.6f ',savemodel{23}(k));
                    fprintf(fid,'%2.6f ',savemodel{24}(k));
                    fprintf(fid,'%2.6f ',savemodel{25}(k));
                    fprintf(fid,'%2.6f ',savemodel{26}(k));
                    fprintf(fid,'%2.6f ',savemodel{27}(k));
                    fprintf(fid,'%2.6f ',savemodel{28}(k));
                    fprintf(fid,'%2.6f ',savemodel{29}(k));
                    fprintf(fid,'%2.6f ',savemodel{30}(k));
                    fprintf(fid,'%2.6f ',savemodel{31}(k));
                    fprintf(fid,'%2.6f\n',savemodel{32}(k));
                end
                fclose(fid);
                waitbar(i/length(modval),wait,['saving ... ',sprintf('%20s',savemodel{2})]);
            end
        end
        close(wait);
    case 5
        datatable   = get(handles.datatable,'Data');
        for i=1:length(modval)
            models(modval(i))     = [];
            datatable(i,:)        = [];
            modval                = modval(i:end)-1;
        end
        set(handles.datatable,'Data',datatable);
        if isempty(datatable)
            modnames    = {};
        else
            modnames{:} = datatable{:,1};
        end
        if isempty(modnames)
            set(handles.datamodellist,'Selected','off');
            set(handles.plotalllist,'Selected','off');
        end
        set(handles.datamodellist,'String',modnames);
        set(handles.plotalllist,'String',modnames);
        set(handles.plotsellist,'Selected','off');
        set(handles.plotsellist,'String',{});
        guidata(hObject, handles);
    case 6
        models = struct('id',{},...
                'name',{},...
                'betamin',{},...
                'betamax',{},...
                'beta',{},...
                'kstart',{},...
                'mustart',{},...
                'Kf',{},...
                'alpha1',{},'alpha3',{},...
                'kappa',{},...
                'c1i',{},'c1c',{},'c2i',{},'c2c',{},'c3i',{},'c3c',{},...
                'nmax',{},...
                'Kun',{},...
                'Kbar',{},...
                'Kiso',{},...
                'Kre',{},...
                'MYun',{},...
                'MYiso',{},...
                'MYre',{},...
                'NUun',{},...
                'NUiso',{},...
                'NUbar',{},...
                'NUre',{},...
                'Aun',{},...
                'Abar',{},...
                'Aiso',{},...
                'unit',{});
        set(handles.datamodellist,'Selected','off');
        set(handles.plotalllist,'Selected','off');
        set(handles.plotsellist,'Selected','off');
        set(handles.datamodellist,'String',{});
        set(handles.plotalllist,'String',{});
        set(handles.plotsellist,'String',{});
        set(handles.datatable,'Data',{});
        
        guidata(hObject, handles);
end
function datatable_CellEditCallback(hObject, eventdata, handles)
function plotaddmod_Callback(hObject, eventdata, handles)

global models;

modvalue    = get(handles.plotalllist,'Value');
sel         = get(handles.plotsellist,'String');
selid       = get(handles.plotsellist,'UserData');
sellen      = length(sel);
sellenid    = length(selid);
for i=1:length(modvalue)
    sel{sellen+i}       = models(modvalue(i)).name;
    selid{sellenid+i}   = models(modvalue(i)).id;
end
set(handles.plotsellist,'String',sel);
set(handles.plotsellist,'UserData',selid);

guidata(hObject, handles);

function plotremovemod_Callback(hObject, eventdata, handles)

modvalue    = get(handles.plotsellist,'Value');
sel         = get(handles.plotsellist,'String');
selid       = get(handles.plotsellist,'UserData');

for i=1:length(modvalue)
    sel(modvalue(i))    = [];
    selid(modvalue(i))  = [];
    modvalue = modvalue(i:end)-1;
end

if isempty(sel)
    set(handles.plotsellist,'Selected','off');
end
set(handles.plotsellist,'String',sel);
set(handles.plotsellist,'UserData',selid);

guidata(hObject, handles);

function plotaddallmod_Callback(hObject, eventdata, handles)

global models;

modsize     = size(models);
sel         = get(handles.plotsellist,'String');
selid       = get(handles.plotsellist,'UserData');
sellen      = length(sel);
sellenid    = length(selid);

for i=1:modsize(2)
    sel{sellen+i}       = models(i).name;
    selid{sellenid+i}   = models(i).id;
end

set(handles.plotsellist,'String',sel);
set(handles.plotsellist,'UserData',selid);

guidata(hObject, handles);

function plotremoveallmod_Callback(hObject, eventdata, handles)

set(handles.plotsellist,'Selected','off');
set(handles.plotsellist,'String',{});
set(handles.plotsellist,'UserData',{});

guidata(hObject, handles);

function doitpopup_Callback(hObject, eventdata, handles)
function doitpopup_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
function modelpopup_Callback(hObject, eventdata, handles)
function modelpopup_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
function doitbutton_CreateFcn(hObject, eventdata, handles)
function plotmodelpopup_Callback(hObject, eventdata, handles)
function plotmodelpopup_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
function plotsellist_Callback(hObject, eventdata, handles)
function plotsellist_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
function chku_Callback(hObject, eventdata, handles)
function chmuu_Callback(hObject, eventdata, handles)
function chkr_Callback(hObject, eventdata, handles)
function chmur_Callback(hObject, eventdata, handles)
function chnuu_Callback(hObject, eventdata, handles)
function chnur_Callback(hObject, eventdata, handles)

function plotitbutton_Callback(hObject, eventdata, handles)

global models;

modvalue    = get(handles.plotsellist,'String');
modid       = get(handles.plotsellist,'UserData');

xmin        = zeros(length(modvalue),1);
xmax        = zeros(length(modvalue),1);
ymin1       = nan(length(modvalue),2);
ymin2       = nan(length(modvalue),2);
ymax1       = nan(length(modvalue),2);
ymax2       = nan(length(modvalue),2);

for i=1:length(modvalue)
    col(i,:)= uisetcolor([models(modid{i}).name,' COLOR']);
end

if get(handles.chku,'Value')==1 || get(handles.chkr,'Value')==1 || ...
    get(handles.chmuu,'Value')==1 || ...
    get(handles.chmur,'Value')==1
    fig1        = figure(10);
    set(fig1,'Name','modulus plot');
    title1      = 'Models: ';
    hold on;
    for i=1:length(modvalue)
        if i==length(modvalue)
            title1 = [title1,' ',models(modid{i}).name,' | '];
        else
            title1 = [title1,' ',models(modid{i}).name,', '];
        end
        xmin(i) = min(models(modid{i}).beta);
        xmax(i) = max(models(modid{i}).beta);
        gr = hggroup;
        if get(handles.chku,'Value')==1
            h1 = plot(models(modid{i}).beta,models(modid{i}).Kun,'Color',col(i,:),'Parent',gr);
            ymin1(i,1)   = min(models(modid{i}).Kun);
            ymax1(i,1)   = max(models(modid{i}).Kun);
        end
        if get(handles.chkr,'Value')==1
            h2 = plot(models(modid{i}).beta,models(modid{i}).Kre,'Color',col(i,:),'LineStyle','--','Parent',gr);
            ymin1(i,2)   = min(models(modid{i}).Kre);
            ymax1(i,2)   = max(models(modid{i}).Kre);
        end
        if get(handles.chmur,'Value')==1
            h4 = plot(models(modid{i}).beta,models(modid{i}).MYre,'Color',col(i,:),'LineStyle','--','Parent',gr);
            ymin2(i,2)   = min(models(modid{i}).MYre);
            ymax2(i,2)   = max(models(modid{i}).MYre);
        end
        if get(handles.chmuu,'Value')==1
            h3 = plot(models(modid{i}).beta,models(modid{i}).MYun,'Color',col(i,:),'Parent',gr);
            ymin2(i,1)   = min(models(modid{i}).MYun);
            ymax2(i,1)   = max(models(modid{i}).MYun);
        end
        set(get(get(gr,'Annotation'),'LegendInformation'),...
            'IconDisplayStyle','on');
        if get(handles.chku,'Value')==1
            set(h1,'LineStyle','-')
        end
        if get(handles.chkr,'Value')==1
            set(h2,'LineStyle','--')
        end
        if get(handles.chmuu,'Value')==1
            set(h3,'LineStyle',':')
        end
        if get(handles.chmur,'Value')==1
            set(h4,'LineStyle','-.')
        end
    end
    if get(handles.chku,'Value')==1 || get(handles.chkr,'Value')==1
        text(min(xmin)+.03*max(xmax),max(max(ymax1))+.02*max(max(ymax1)),'K','FontSize',14)
    end
    if get(handles.chmuu,'Value')==1 || get(handles.chmur,'Value')==1
        text(min(xmin)+.03*max(xmax),max(max(ymax2))+.02*max(max(ymax2)),'\mu','FontSize',14)
    end
    hold off;
    set(gca,'XLim',[min(xmin)-.05*max(xmax) ...
        max(xmax)+.1*max(xmax)]);
    set(gca,'YLim',[min(min([ymin1 ymin2]))-.05*max(max([ymax1 ymax2])) ...
        max(max([ymax1 ymax2]))+.1*max(max([ymax1 ymax2]))]);
    xlabel('TOTAL MELT FRACTION \beta');
    ylabel(['MODULUS [',models(modid{1}).unit,']']);
    if get(handles.chku,'Value')==1
        if get(handles.chkr,'Value')==1 || get(handles.chmuu,'Value')==1 || get(handles.chmur,'Value')==1
            title1 = [title1,' K_u (solid), '];
        else
            title1 = [title1,' K_u (solid)'];
        end
    end
    if get(handles.chkr,'Value')==1
        if get(handles.chku,'Value')==1 && (get(handles.chmuu,'Value')==1 || get(handles.chmur,'Value')==1)
            title1 = [title1,' K_r (dashed), '];
        else
            title1 = [title1,' K_r (dashed)'];
        end
    end
    if get(handles.chmuu,'Value')==1
        if get(handles.chmur,'Value')==1 && (get(handles.chku,'Value')==1 || get(handles.chkr,'Value')==1)
            title1 = [title1,' \mu_u (dotted), '];
        else
            title1 = [title1,' \mu_u (dotted)'];
        end
    end
    if get(handles.chmur,'Value')==1
        title1 = [title1,' \mu_r (dash-dot)'];
    end
    
    title(title1);
    leg = legend(modvalue);
    lines = findobj(leg,'Type','line');
    for k=1:length(lines) 
       set(lines(k),'LineStyle','-'); 
    end
end
if get(handles.chnuu,'Value')==1 || get(handles.chnur,'Value')==1
    fig2        = figure(11);
    set(fig2,'Name','poisson ratio plot');
    title2      = 'Models: ';
    hold on;
    for i=1:length(modvalue)
        if i==length(modvalue)
            title2 = [title2,' ',models(modid{i}).name,' | '];
        else
            title2 = [title2,' ',models(modid{i}).name,', '];
        end
        xmin(i) = min(models(modid{i}).beta);
        xmax(i) = max(models(modid{i}).beta);
        gr = hggroup;
        if get(handles.chnur,'Value')==1
            h6 = plot(models(modid{i}).beta,models(modid{i}).NUre,'Color',col(i,:),'LineStyle','--','Parent',gr);
            ymin1(i,2)   = min(models(modid{i}).NUre);
            ymax1(i,2)   = max(models(modid{i}).NUre);
        end
        if get(handles.chnuu,'Value')==1
            h5 = plot(models(modid{i}).beta,models(modid{i}).NUun,'Color',col(i,:),'Parent',gr);
            ymin1(i,1)   = min(models(modid{i}).NUun);
            ymax1(i,1)   = max(models(modid{i}).NUun);
        end
        set(get(get(gr,'Annotation'),'LegendInformation'),...
            'IconDisplayStyle','on');
        if get(handles.chnuu,'Value')==1
            set(h5,'LineStyle','-')
        end
        if get(handles.chnur,'Value')==1
            set(h6,'LineStyle','--')
        end
    end
    hold off;
    set(gca,'XLim',[min(xmin)-.05*max(xmax) ...
        max(xmax)+.1*max(xmax)]);
    set(gca,'YLim',[min(min(ymin1))-.05*max(max(ymax1)) ...
        max(max(ymax1))+.1*max(max(ymax1))]);
    xlabel('TOTAL MELT FRACTION \beta');
    ylabel('POISSON RATIO');
    if get(handles.chnuu,'Value')==1
        if get(handles.chnur,'Value')==1 
            title2 = [title2,' \nu_u (solid), '];
        else
            title2 = [title2,' \nu_u (solid)'];
        end
    end
    if get(handles.chnur,'Value')==1
        title2 = [title2,' \nu_r (solid)'];
    end
    title(title2);
    legend(modvalue,'LineStyleOrder','-');
end
function plotalllist_Callback(hObject, eventdata, handles)
function plotalllist_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
function funit_Callback(hObject, eventdata, handles)
function funit_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in datamodellist.
function datamodellist_Callback(hObject, eventdata, handles)
% hObject    handle to datamodellist (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns datamodellist contents as cell array
%        contents{get(hObject,'Value')} returns selected item from datamodellist


% --- Executes during object creation, after setting all properties.
function datamodellist_CreateFcn(hObject, eventdata, handles)
% hObject    handle to datamodellist (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
