function buttonimportLIB(hObject, eventdata, emptybutton, externbutton, t, Info, Axes, parent, basicfsz, sty, calcs, basichi)

data = get(emptybutton,'UserData');

fig = figure;
pause(.1);

pfp = get(parent,'Position');
scrsz = get(0,'ScreenSize');
basichi = 7.5*basichi;

set(fig,'Position',[pfp(1)+1/4*pfp(3), pfp(2)+pfp(4)*1/3, pfp(3)/2, pfp(4)/3],'Name','Load a new entry from workspace',...
    'NumberTitle','off','MenuBar','none','Toolbar','none','color','white');
pause(.1);

set(fig,'Name','Load a new pure component spectrum for library','NumberTitle','off','MenuBar','none','Toolbar','none','color','white');

Textfield(1) = uicontrol('Style','text','String','Channels:','HorizontalAlignment','left',...
    'Fontsize',2.5*basicfsz,'BackgroundColor','white','Units','normalized','FontName',sty,'Position',[0.01, 0.8, 0.2, basichi]);

Textfield(2) = uicontrol('Style','text','String','Spectrum:','HorizontalAlignment','left',...
    'Fontsize',2.5*basicfsz,'BackgroundColor','white','Units','normalized','FontName',sty,'Position',[0.01, 0.625, 0.2, basichi]);

Textfield(3) = uicontrol('Style','text','String','Name:','HorizontalAlignment','left',...
    'Fontsize',2.5*basicfsz,'BackgroundColor','white','Units','normalized','FontName',sty,'Position',[0.01, 0.45, 0.2, basichi]);

Textfield(4) = uicontrol('Style','edit','String','','HorizontalAlignment','left',...
    'Fontsize',2.5*basicfsz,'BackgroundColor',[.9 .9 .9],'Units','normalized','FontName',sty,'Position',[0.23, 0.45, 0.75, basichi]);

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

popup(1) = uicontrol('Style','popup','String',{},'HorizontalAlignment','left',...
    'Fontsize',2.5*basicfsz,'BackgroundColor','white','Units','normalized','FontName',sty,'Position',[0.23, 0.8, 0.75, basichi]);

popup(2) = uicontrol('Style','popup','String',{},'HorizontalAlignment','left',...
    'Fontsize',2.5*basicfsz,'BackgroundColor','white','Units','normalized','FontName',sty,'Position',[0.23, 0.625, 0.75, basichi]);

vars = evalin('base','who');
if ~isempty(vars)
    varsx = {vars{:}, 'not available'};
    set(popup(1),'String',varsx);
    set(popup(2),'String',vars);
else
    varsx = vars;
    set(popup(1),'String',{'-empty-'});
    set(popup(2),'String',{'-empty-'});
end

inneremptybutton = uicontrol('Style','pushbutton','Position',[0.02, 0.9, 0.095, basichi],'Visible','off','Callback',{@emptybutton_CB});

innerdata.vars = vars;
innerdata.varsx = varsx;

set(inneremptybutton,'UserData',innerdata);

Button(1) = uicontrol('Style','pushbutton','UserData',0,'String','Cancel','HorizontalAlignment','center','Fontsize',2.5*basicfsz,'FontWeight','bold',...
        'Units','normalized','FontName',sty,'Position',[0.025, 0.075, 0.3, 0.2],...
        'Callback',{@innerbuttoncancel, fig});

Button(2) = uicontrol('Style','pushbutton','UserData',0,'String','Reload workspace','HorizontalAlignment','center','Fontsize',2.5*basicfsz,'FontWeight','bold',...
        'Units','normalized','FontName',sty,'Position',[0.35, 0.075, 0.3, 0.2],...
        'Callback',{@innerbuttonreload, inneremptybutton, Textfield, popup});

Button(3) = uicontrol('Style','pushbutton','UserData',0,'String','Add to library','HorizontalAlignment','center','Fontsize',2.5*basicfsz,'FontWeight','bold',...
        'Units','normalized','FontName',sty,'Position',[0.675, 0.075, 0.3, 0.2],...
        'Callback',{@innerbuttonadd, inneremptybutton, emptybutton, externbutton, fig, t, Axes, popup, Textfield, Info, calcs});

set(popup(2), 'Callback',{@innerCBselect, inneremptybutton, Textfield, popup});

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

function innerbuttonreload(hObject, eventdata, inneremptybutton, Textfield, popup)
innerdata = get(inneremptybutton,'UserData');
vars = evalin('base','who');
if ~isempty(vars)
    varsx = {vars{:}, 'not available'};
    set(popup(1),'String',varsx,'Value',1);
    set(popup(2),'String',vars,'Value',1);
else
    varsx = vars;
    set(popup(1),'String',{'-empty-'},'Value',1);
    set(popup(2),'String',{'-empty-'},'Value',1);
end
innerdata.vars = vars;
innerdata.varsx = varsx;
set(Textfield(4), 'String', '');
set(inneremptybutton,'UserData',innerdata);



function innerCBselect(hObject, eventdata, inneremptybutton, Textfield, popup)
innerdata = get(inneremptybutton,'UserData');
strce = innerdata.vars(get(popup(2),'Value'));
set(Textfield(4), 'String', strce{1});


function innerbuttonadd(hObject, eventdata, inneremptybutton, emptybutton, externbutton, fig, t, Axes, popup, Textfield, Info, calcs)
data = get(emptybutton,'UserData');
lib = data.lib;
innerdata = get(inneremptybutton,'UserData');

a = evalin('base',innerdata.vars{get(popup(2),'Value')});
if get(popup(1),'Value') == length(get(popup(1),'String'))
    x = (1:length(a))';
else
    x = evalin('base',innerdata.varsx{get(popup(1),'Value')});
end
if isnumeric(x) == 0
    set(Textfield(5), 'String' ,'Import not successful: channels is not numeric','Foregroundcolor','red');
    return
end
if min(size(x)) > 1
    set(Textfield(5), 'String' ,'Import not successful: channels is a matrix','Foregroundcolor','red');
    return
end
if isnumeric(a) == 0
    set(Textfield(5), 'String' ,'Import not successful: spectrum is not numeric','Foregroundcolor','red');
    return
end
if min(size(x)) > 1
    set(Textfield(5), 'String' ,'Import not successful: spectrum is a matrix','Foregroundcolor','red');
    return
end
if length(x) ~= length(a)
    set(Textfield(5), 'String' ,'Import not successful: dimensions of x and spectrum are different','Foregroundcolor','red');
    return
end

set(Textfield(5), 'String' ,'Import successful','Foregroundcolor','black');

cln = clock;
cln = [num2str(cln(4),'%2.2d'), ':', num2str(cln(5),'%2.2d'), ':', num2str(round(cln(6)),'%2.2d')];

no = length(lib);

lib(no+1).name = get(Textfield(4), 'String');
lib(no+1).x = x;
lib(no+1).a = a;
lib(no+1).date = date;
lib(no+1).clock = cln;
lib(no+1).module = 'extern';
lib(no+1).dataset = 'n.n.';
lib(no+1).version = NaN;

dat = [];
for i=1:length(lib)
    dat{i,1} = false;
    dat{i,2} = lib(i).name;
    dat{i,3} = num2str(length(lib(i).a),'%d');
    dat{i,4} = lib(i).date;
    dat{i,5} = lib(i).module;
    dat{i,6} = 'original';
end

set(t, 'Data', dat);

for i=2:7
    set(Info(i),'String','');
end

plot(Axes, x, a, 'Linewidth', 2);
onoffButton(externbutton,[],[1 3 4]);

data.isselected = 0;
data.lib = lib;

save([calcs,'/FACPACKlibrary.mat'],'lib');
set(emptybutton,'UserData',data);

close(fig);
