function buttonloadspectraCA4(hObject, eventdata, parentfig, Empty_Button, Buttons, Footnote, AxesC, AxesA, AxesSOLC, AxesSOLA, Polysettings, uonoff, uonoffC, plotopt, lvText, lvSlider, basicfsz, basichi, calclib, lcs, sty)
data = get(Empty_Button,'UserData');

data.givena.is = 0;
data.givena.s = 0;
data.givena.adaptedis = 0;
data.givena.adaptedI = [];
data.givena.A = [];
data.givena.afs = [];

data = get(Empty_Button,'UserData');

fig = figure;
pause(.1);

scrsz = get(0,'ScreenSize');

set(fig,'Position',[scrsz(3)/4 scrsz(4)*3/8 scrsz(3)/2 scrsz(4)/4]);
pause(.1);

set(fig,'Name','Load spectrum from *.mat file or library','NumberTitle','off','MenuBar','none','Toolbar','none','color','white');

basicfsz = 2*basicfsz;
basichi = 5*basichi;

Textfield(1) = uicontrol('Style','text','String','current data:','HorizontalAlignment','left','Fontsize',basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.01, 0.5, 0.15, basichi]);

str = ['channels interval [',num2str(min(data.x),'%5.2f'),', ',num2str(max(data.x),'%5.2f'),'],  dimension: ',num2str(length(data.x))];
Textfield(2) = uicontrol('Style','text','String',str,'HorizontalAlignment','left','Fontsize',basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.16, 0.5, 0.73, basichi]);

Textfield(3) = uicontrol('Style','text','String','loaded data:','HorizontalAlignment','left','Fontsize',basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.01, 0.325, 0.15, basichi]);

Textfield(4) = uicontrol('Style','text','String','','HorizontalAlignment','left','Fontsize',basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.16, 0.325, 89, basichi]);

Textfield(5) = uicontrol('Style','text','String','','HorizontalAlignment','center','Fontsize',basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.01, 0.2, 0.98, basichi]);

load([calclib,'/FACPACKlibrary.mat']);
if (exist('lib') == 0) || (length(lib) == 0)
    isnempty = 'off';
    dat = {'empty'};
else
    for i=1:length(lib)
        dat{i} = lib(i).name;
    end
    isnempty = 'on';
end

popup(1) = uicontrol('Style','popupmenu','String',dat,'HorizontalAlignment','left','Fontsize',basicfsz,'BackgroundColor','white',...
    'Units','normalized','FontName',sty,'Position',[0.51, 0.83, 0.48, 0.12], 'Value',1, 'Enable', isnempty);


inneremptybutton = uicontrol('Style','pushbutton','Position',[0.02, 0.9, 0.095, basichi],'Visible','off');
datain.isload = 0;
set(inneremptybutton,'UserData',datain);

Button(1) = uicontrol('Style','pushbutton','UserData',0,'String','Cancel','HorizontalAlignment','center','Fontsize',basicfsz,...
    'FontWeight','bold','Units','normalized','FontName',sty,'Position',[0.01, 0.01, 0.32, basichi],'Enable','on',...
    'Callback',{@innerbuttoncancel, fig});

Button(2) = uicontrol('Style','pushbutton','UserData',0,'String','Adapt & import','HorizontalAlignment','center','Fontsize',basicfsz,...
    'FontWeight','bold','Units','normalized','FontName',sty,'Position',[0.34, 0.01, 0.32, basichi],'Enable','off',...
    'Callback',{@innerbuttonadapt, Empty_Button, inneremptybutton, fig, Footnote, AxesA, AxesC, AxesSOLA, AxesSOLC, uonoff, uonoffC, plotopt, lvText, lvSlider, lcs});

Button(3) = uicontrol('Style','pushbutton','UserData',0,'String','Import','HorizontalAlignment','center','Fontsize',basicfsz,...
    'FontWeight','bold','Units','normalized','FontName',sty,'Position',[0.67, 0.01, 0.32, basichi],'Enable','off',...
    'Callback',{@innerbuttonimport, Empty_Button, inneremptybutton, fig, Footnote, AxesA, AxesC, AxesSOLA, AxesSOLC, uonoff, uonoffC, plotopt, lvText, lvSlider, lcs});

Button(4) = uicontrol('Style','pushbutton','UserData',0,'String','Load from *.mat file','HorizontalAlignment','center',...
    'Fontsize',basicfsz,'FontWeight','bold','Units','normalized','FontName',sty,'Position',[0.01, 0.7, 0.48, 0.25],...
    'Callback',{@innerbuttonload, Empty_Button, inneremptybutton, Button, Textfield});

Button(5) = uicontrol('Style','pushbutton','UserData',0,'String','Load from library','HorizontalAlignment','center',...
    'Fontsize',basicfsz,'FontWeight','bold','Units','normalized','FontName',sty,'Position',[0.51, 0.7, 0.48, 0.12], 'Enable', isnempty,...
    'Callback',{@innerbuttonloadfromlib, Empty_Button, inneremptybutton, Button, popup, Textfield, calclib});

set(popup(1), 'Callback',{@innerbuttonreset, inneremptybutton, Button, Textfield});

if (exist('lib') == 0) || (length(lib) == 0)
    set(Button(5), 'Enable', 'off');
end

set(Empty_Button,'UserData',data);


function innerbuttonreset(hobject, eventdata, inneremptybutton, Button, Textfield)
onoffButton(Button,[],2:4);
set(Textfield(4), 'String', '');
set(Textfield(5), 'String','New selection in popup menu was done: Please press LOAD FROM LIBRARY next.','ForegroundColor','k');


function innerbuttonadapt(hobject, eventdata, Empty_Button, inneremptybutton, fig, Footnote, AxesA, AxesC, AxesSOLA, AxesSOLC, uonoff, uonoffC, plotopt, lvText, lvSlider, lcs)
data = get(Empty_Button,'UserData');
datain = get(inneremptybutton,'UserData');

I = 1:size(data.D,2);
if min(data.x) <= min(datain.x)
    i1 = find(data.x >= min(datain.x),1);
else
    i1 = 1;
end
if max(data.x) >= max(datain.x)
    i2 = find(data.x <= max(datain.x),1,'last');
else
    i2 = size(data.D,2);
end

data.givena.s = size(datain.a,1);
data.givena.A = interp1(datain.x, datain.a, data.x(I(i1:i2)))';
data.givena.line = [];
data.givena.adaptedis = 1;
data.givena.adaptedI = [i1 i2];

for i=1:data.givena.s
    t = data.givena.A(i,:)*data.V(i1:i2,1:4);
    data.givena.afs(i,:) = t(2:4)/t(1);
    data.givena.line(i,:) = data.givena.A(i,:)/max(data.givena.A(i,:));
    data.givena.A(i,:) = data.givena.A(i,:)/t(1);
end

if data.givena.s == 1
    set(Footnote,'String',[' Spectrum import: ', num2str(data.givena.s), ' spectrum loaded. '],'ForegroundColor','k');
else
    set(Footnote,'String',[' Spectra import: ', num2str(data.givena.s), ' spectra loaded. '],'ForegroundColor','k');
end

data.givena.is = 1;
set(Empty_Button,'UserData',data);

if (data.motion == 0) && (data.motionC == 0)
    plotafsCA4(AxesA, AxesC, AxesSOLA, AxesSOLC, data.afsA, data.afsC, data.FIRPOLA, data.FIRPOLC, data.INNPOLA, data.INNPOLC, data.isFP, data.isIP, data.isAFS, lcs, [0 0 0], [-1 -1], plotopt);
    
    plotaddsCA4(AxesA, AxesC, AxesSOLA, AxesSOLC, data.afsA, data.afsC, data.fixA, data.fixC, data.U*data.S, data.V', data.choosen, data.x, data.t,...
    data.givena, data.givenc, data.S(2:4,2:4),data.S(1,1), [], [], [], [], lcs, [], [], plotopt, 0, 0, [])
else
    liveviewCA4(hObject, eventdata, Empty_Button, AxesC, AxesA, AxesSOLC, AxesSOLA, uonoff, uonoffC, plotopt, lvText, lvSlider, -1, lcs, [0 0 0])
end
close(fig);




function innerbuttonimport(hobject, eventdata, Empty_Button, inneremptybutton, fig, Footnote, AxesA, AxesC, AxesSOLA, AxesSOLC, uonoff, uonoffC, plotopt, lvText, lvSlider, lcs)
data = get(Empty_Button,'UserData');
datain = get(inneremptybutton,'UserData');

data.givena.s = size(datain.a,1);
data.givena.A = datain.a;
data.givena.line = [];
data.givena.adaptedis = 0;
data.givena.adaptedI = [];

for i=1:data.givena.s
    t = data.givena.A(i,:)*data.V(:,1:4);
    data.givena.afs(i,:) = t(2:4)/t(1);
    data.givena.line(i,:) = data.givena.A(i,:)/max(data.givena.A(i,:));
    data.givena.A(i,:) = data.givena.A(i,:)/t(1);
end

if data.givena.s == 1
    set(Footnote,'String',[' Spectrum import: ', num2str(data.givena.s), ' spectrum loaded. '],'ForegroundColor','k');
else
    set(Footnote,'String',[' Spectra import: ', num2str(data.givena.s), ' spectra loaded. '],'ForegroundColor','k');
end

data.givena.is = 1;
set(Empty_Button,'UserData',data);

if (data.motion == 0) && (data.motionC == 0)
    plotafsCA4(AxesA, AxesC, AxesSOLA, AxesSOLC, data.afsA, data.afsC, data.FIRPOLA, data.FIRPOLC, data.INNPOLA, data.INNPOLC, data.isFP, data.isIP, data.isAFS, lcs, [0 0 0], [-1 -1], plotopt);
    
    plotaddsCA4(AxesA, AxesC, AxesSOLA, AxesSOLC, data.afsA, data.afsC, data.fixA, data.fixC, data.U*data.S, data.V', data.choosen, data.x, data.t,...
    data.givena, data.givenc, data.S(2:4,2:4),data.S(1,1), [], [], [], [], lcs, [], [], plotopt, 0, 0, [])
else
    liveviewCA4(hObject, eventdata, Empty_Button, AxesC, AxesA, AxesSOLC, AxesSOLA, uonoff, uonoffC, plotopt, lvText, lvSlider, -1, lcs, [0 0 0])
end
close(fig);


function innerbuttonloadfromlib(hobject, eventdata, Empty_Button, inneremptybutton, Button, popup, Textfield, calclib)
dataout = get(Empty_Button,'UserData');

load([calclib,'/FACPACKlibrary.mat']);

no = get(popup(1),'Value');

datain.x = lib(no).x;
datain.a = lib(no).a;
datain.isload = 1;

set(Textfield(4), 'String', ['channels interval [',num2str(min(datain.x),'%5.2f'),', ',num2str(max(datain.x),'%5.2f'),'],  dimension: ',num2str(length(datain.x))],...
    'ForegroundColor','k');

if length(datain.x) == length(dataout.x)
    if norm(datain.x-dataout.x,'inf') < 1.E-14
        onoffButton(Button,3,2);
        set(Textfield(5), 'String','The spectrum is ready for import.','ForegroundColor','r');
    else
        onoffButton(Button,2,3);
        set(Textfield(5), 'String','The channels does not fit: The spectrum needs to be adapted.','ForegroundColor','r');
    end
else
    if (min(dataout.x) < max(datain.x)) && (min(datain.x) < max(dataout.x))
        set(Textfield(5), 'String','The channels does not fit: The spectrum needs to be adapted.','ForegroundColor','r');
        onoffButton(Button,2,3);
    else
        set(Textfield(5), 'String','ERROR: The channels does not fit at all.','ForegroundColor','r');
        datain.isload = 0;
    end
end
set(inneremptybutton,'UserData',datain);


function innerbuttonload(hObject, eventdata, Empty_Button, inneremptybutton, Button, Textfield)
dataout = get(Empty_Button,'UserData');

if ispc
    dlm = '\';
else
    dlm = '/';
end

actpwd = pwd;
cd(dataout.PathName);
[FileName, PathName, FilterIndex] = uigetfile('*.mat','Select .mat file including the data Matrix D');
cd(actpwd);

if FilterIndex == 0
    return
end

onoffButton(Button,[],2:3);

load([PathName FileName]);
if ~exist('a') && exist('A')
    a = A;
end
if ~exist('x') && exist('X')
    x = X;
end
if exist('a')*exist('x') == 0
    set(Textfield(4), 'String', 'ERROR: Missing spectrum "a" or channels vector "x".','ForegroundColor','r');
elseif (length(x) ~= size(a,2)) && (length(x) == size(a,1))
    a = a';
end
if (exist('a')*exist('x') == 1)
    if (length(x) == size(a,2))
        set(Textfield(4), 'String', ['channels interval [',num2str(min(x),'%5.2f'),', ',num2str(max(x),'%5.2f'),'],  dimension: ',num2str(length(x))],...
        'ForegroundColor','k');
        
        if x(1) > x(2)
            x = x(end:-1:1);
            a = a(:,end:-1:1);
        end
        if size(x,1) < size(x,2)
            x = x';
        end
        if length(x) == length(dataout.x)
            if norm(x-dataout.x,'inf') < 1.E-14
                onoffButton(Button,3,[]);
                if size(a,1) == 1
                    set(Textfield(5), 'String','The spectrum is ready for import.','ForegroundColor','r');
                else
                    set(Textfield(5), 'String','The spectra are ready for import.','ForegroundColor','r');
                end
            else
                onoffButton(Button,2,[]);
                if size(a,1) == 1
                    set(Textfield(5), 'String','The channels does not fit: The spectrum needs to be adapted.','ForegroundColor','r');
                else
                    set(Textfield(5), 'String','The channels does not fit: The spectra need to be adapted.','ForegroundColor','r');
                end
            end
        else
            if (min(dataout.x) < max(x)) && (min(x) < max(dataout.x))
                if size(a,1) == 1
                    set(Textfield(5), 'String','The channels does not fit: The spectrum needs to be adapted.','ForegroundColor','r');
                else
                    set(Textfield(5), 'String','The channels does not fit: The spectra need to be adapted.','ForegroundColor','r');
                end
                onoffButton(Button,2,[]);
            else
                set(Textfield(5), 'String','ERROR: The channels does not fit at all.','ForegroundColor','r');
            end
        end
        
        datain.a = a;
        datain.x = x;
        datain.isload = 1;
        
        set(inneremptybutton,'UserData',datain);
        return;
    else
        set(Textfield(4), 'String', ['ERROR: length(x): ',num2str(length(x),'%d'),' does not fit with size(a): ',num2str(size(a),'%d,%d')'.'],...
        'ForegroundColor','r');
    end
end
onoffButton(Button,2:3,[]);
datain.isloadin = 0;
set(inneremptybutton,'UserData',datain);


function innerbuttoncancel(hobject, eventdata, fig)
close(fig);

