function subGUI4AFS(version,nr,basicfsz)
% The toolbox FACPACK, which includes the function subGUI4AFS, provides software 
% for the computation of multi-component factorizations and the area of feasible solutions.
% Copyright (c) 2013 Mathias Sawall, University of Rostock, Department of Mathematics.
% 
% The software can be used for academic, research and other similar noncommercial uses. The user
% acknowledges that this software is still in the development stage and that it is provided by the
% copyright holders and contributors "as is" and any express or implied warranties, fitness for a
% particular purpose are disclaimed. In no event shall the copyright owner or contributors be liable
% for any direct, indirect, incidental, special, exemplary, or consequential damages.
% 
% The copyright holders provide no reassurances that the source code provided does not infringe any
% patent, copyright, or any other intellectual property rights of third parties. The copyright
% holders disclaim any liability to any recipient for claims brought against recipient by any third
% party for infringement of that parties intellectual property rights.
% 
% The user has to respect the lapack license, see http://www.netlib.org/lapack/LICENSE.txt, and
% the license for the ACM routine nl2sol, see http://www.acm.org/publications/policies/softwarecrnotice,
% which are used in the associated C-code.

fname = mfilename;
calcf = mfilename('fullpath');
calcf = calcf(1:end-length(fname));
calcp = [calcf, 'subfcnsAFS'];
calclib = [calcf, 'subfcnsLIB'];
path(path, calcf);
path(path, calcp);
path(path, calclib);

scrsz = get(0,'ScreenSize');
fig = figure(nr);
pause(.1);
set(fig,'Position',[1/20*scrsz(3:4) 9/10*scrsz(3:4)]);
% pause(.5);
set(fig,'Name',['FACPACK (Computation of the AFS by polygon inflation, version ',num2str(version,'%2.2f'), ')'],...
    'NumberTitle','off','MenuBar','none','Toolbar','none','color','white','Visible','off');

if ispc
%     basicfsz = 5;
    basichi = 0.025;
else
%     basicfsz = 6.5;
    basichi = 0.025;
end
basicfsz = basicfsz*scrsz(4)/1080;

Styles = {'bitstream charter';'clean';'Courier';'courier 10 pitch';'frank ruehl';'Helvetica';'Helvetica-Narrow';'itc bookman';'urw bookman l'};
sty = Styles{6};
bgc = 0.95*ones(3,1);
% lcs = [0 0 1;0 0.5 0;1 0 0;0 0.5 0.5;0.5 0 0.5;0.5 0.5 0];
lcs = [0 0.4470 0.7410; 0.8500 0.3250 0.0980; 0.9290 0.6940 0.1250; 0.4940 0.1840 0.5560; 0.4660 0.6740 0.1880; 0.3010 0.7450 0.9330];
LCS = [0.85 0.9 1; 0.995 0.8250 0.75; 1 0.9 0.75; 0.9 0.6 1; 0.7 1 0.5; 0.7 0.9 1];

% The title names
titles(1) = uicontrol('Style','text','String','Data window','HorizontalAlignment','center','Fontsize',3*basicfsz,'BackgroundColor','w',...
    'Units','normalized','FontName',sty,'Position',[0.01, 0.96, 0.498, basichi]);

titles(2) = uicontrol('Style','text','String','NMF window','HorizontalAlignment','center','Fontsize',3*basicfsz,'BackgroundColor','w',...
    'Units','normalized','FontName',sty,'Position',[0.501, 0.96, 0.498, basichi]);

titles(3) = uicontrol('Style','text','String',' AFS window','HorizontalAlignment','center','Fontsize',3*basicfsz,'BackgroundColor','w',...
    'Units','normalized','FontName',sty,'Position',[0.01, 0.49, 0.498, basichi]);

titles(4) = uicontrol('Style','text','String',' Factor window','HorizontalAlignment','center','Fontsize',3*basicfsz,'BackgroundColor','w',...
    'Units','normalized','FontName',sty,'Position',[0.501, 0.49, 0.498, basichi]);

titles(5) = uicontrol('Style','text','String','filename','HorizontalAlignment','center','Fontsize',1.5*basicfsz,'BackgroundColor','w',...
    'Units','normalized','FontName',sty,'Position',[0.01, 0.94, 0.98, .9*basichi]);


% The infomation-text-field
Footnote = uicontrol('Style','text','String','Computational-time info','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.002, 0.002, 0.5, basichi]);


% The helpField for help-text
Helpfield(1) = uicontrol('Style','Checkbox','String',' Help: ','HorizontalAlignment','right','Fontsize',2*basicfsz,'FontWeight','bold',...
    'BackgroundColor','w','Units','normalized','FontName',sty,'Position',[0.632, 0.002, 0.06, basichi], 'Value', 1);

Helpfield(2) = uicontrol('Style','text','String','  ','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'BackgroundColor',bgc,'Units','normalized','FontName',sty,'Position',[0.67, 0.002, 0.329, basichi]);


% The data-information-text-fields
Datainfo(1) = uicontrol('Style','text','String','Data info','HorizontalAlignment','center','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.02, 0.81, 0.1, basichi]);

Datainfo(2) = uicontrol('Style','text','String',' spectra: ','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.02, 0.78, 0.1, basichi]);

Datainfo(3) = uicontrol('Style','text','String',' channels: ','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.02, 0.75, 0.1, basichi]);


% The SVD-information-text-fields
SVDinfo(1) = uicontrol('Style','text','String','      svd-info','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.02, 0.69, 0.1, basichi]);

SVDinfo(2) = uicontrol('Style','text','String',' s(1):','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName','symbol','Position',[0.02, 0.66, 0.1, basichi],...
    'TooltipString', sprintf('First singular value, indicates no components.'));

SVDinfo(3) = uicontrol('Style','text','String',' s(2): ','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName','symbol','Position',[0.02, 0.63, 0.1, basichi],...
    'TooltipString', sprintf('Second singular value, indicates no components.'));

SVDinfo(4) = uicontrol('Style','text','String',' s(3): ','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName','symbol','Position',[0.02, 0.6, 0.1, basichi],...
    'TooltipString', sprintf('Third singular value, indicates no components.'));

SVDinfo(5) = uicontrol('Style','text','String',' s(4): ','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName','symbol','Position',[0.02, 0.57, 0.1, basichi],...
    'TooltipString', sprintf('Fourth singular value, indicates no components.'));


% The NMF-parameter-field
NMFparameter(1) = uicontrol('Style','text','String',' singl vcts:','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.52, 0.83, 0.07, basichi]);

NMFparameter(2) = uicontrol('Style','edit','String',' 2/3','HorizontalAlignment','right','Fontsize',2*basicfsz,...
    'Units','normalized','FontName',sty,'Position',[0.595, 0.83, 0.025, basichi],...
    'TooltipString', sprintf('Number of right singular vectors to\nuse for the computation of an initial NMF.\nMinimal: no components. Maximal: 5.'));


% The NMF-information-text-fields
NMFinfo(1) = uicontrol('Style','text','String','NMF info','HorizontalAlignment','center','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.52, 0.66, 0.1, basichi]);

NMFinfo(2) = uicontrol('Style','text','String',' min(C): ','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.52, 0.63, 0.1, basichi]);

NMFinfo(3) = uicontrol('Style','text','String',' min(S): ','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.52, 0.60, 0.1, basichi]);


% The control-parameters for the polygon inflation
x_polypar = 0.01;
y_polypar = 0.45;
l_polypar = 0.07;
Polyparameter(1) = uicontrol('Style','text','String',' e-neg. entr.: ','HorizontalAlignment','right','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[x_polypar, y_polypar, l_polypar, basichi]);

Polyparameter(2) = uicontrol('Style','text','String',' e-bound: ','HorizontalAlignment','right','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[x_polypar, y_polypar-0.03, l_polypar, basichi]);

Polyparameter(3) = uicontrol('Style','text','String',' d-stopping: ','HorizontalAlignment','right','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[x_polypar, y_polypar-0.06, l_polypar, basichi]);

Polyparameter(4) = uicontrol('Style','text','String',' max fcls:','HorizontalAlignment','right','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[x_polypar, y_polypar-0.09, l_polypar, basichi]);

Polyparameter(5) = uicontrol('Style','text','String',' max edges: ','HorizontalAlignment','right','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[x_polypar, y_polypar-0.12, l_polypar, basichi]);


% The control-values for the polygon inflation
x_polyset = 0.09;
l_polyset = 0.07;
yv = get(Polyparameter(1),'Position');
Polysettings(1) = uicontrol('Style','edit','String',' 1.0e-12','HorizontalAlignment','right','Fontsize',2*basicfsz,...
    'Units','normalized','FontName',sty,'Position',[x_polyset, yv(2), l_polyset, basichi],...
    'TooltipString', sprintf('Acceptable relative negativeness for\nan NMF.\nValue greater than 0.'));

yv = get(Polyparameter(2),'Position');
Polysettings(2) = uicontrol('Style','edit','String',' 1.0e-03','HorizontalAlignment','right','Fontsize',2*basicfsz,...
    'Units','normalized','FontName',sty,'Position',[x_polyset, yv(2), l_polyset, basichi],...
    'TooltipString', sprintf('Precision at the boundary.'));

yv = get(Polyparameter(3),'Position');
Polysettings(3) = uicontrol('Style','edit','String',' 1.0e-03','HorizontalAlignment','right','Fontsize',2*basicfsz,...
    'Units','normalized','FontName',sty,'Position',[x_polyset, yv(2), l_polyset, basichi],...
    'TooltipString', sprintf('Stopping precision.'));

yv = get(Polyparameter(4),'Position');
Polysettings(4) = uicontrol('Style','edit','String',' 5000','HorizontalAlignment','right','Fontsize',2*basicfsz,...
    'Units','normalized','FontName',sty,'Position',[x_polyset, yv(2), l_polyset, basichi],...
    'TooltipString', sprintf('Maximal number of calls\nfor the target function.'));

yv = get(Polyparameter(5),'Position');
Polysettings(5) = uicontrol('Style','edit','String',' 500','HorizontalAlignment','right','Fontsize',2*basicfsz,...
    'Units','normalized','FontName',sty,'Position',[x_polyset, yv(2), l_polyset, basichi],...
    'TooltipString', sprintf('Maximal number of edges for the polygons.'));


% The AFS-information-text-fields
yv = get(Polyparameter(5),'Position');
AFSinfo(1) = uicontrol('Style','text','String',' edges:','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.01, yv(2)-.165, 0.15, basichi]);

yv = get(AFSinfo(1),'Position');
AFSinfo(2) = uicontrol('Style','text','String',' funcalls: ','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.01, yv(2)-0.03, 0.15, basichi]);

AFSinfo(3) = uicontrol('Style','text','String',' biseccalls: ','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.01, yv(2)-0.06, 0.15, basichi]);


% The control-parameters for the AFS-transformation-plot
xv = get(NMFinfo(1),'Position');
yv = get(Polyparameter(1),'Position');
Plotparameter(1) = uicontrol('Style','text','String',' bnd.-pts.:','HorizontalAlignment','right','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[xv(1), yv(2), .06, basichi]);

Plotparameter(2) = uicontrol('Style','text','String',' stepsize x:','HorizontalAlignment','right','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[xv(1), yv(2)-0.03, .06, basichi]);

Plotparameter(3) = uicontrol('Style','text','String',' stepsize y:','HorizontalAlignment','right','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[xv(1), yv(2)-0.06, .06, basichi]);


% The control-values for the AFS-transformation-plot
yv = get(Plotparameter(1),'Position');
Plotsettings(1) = uicontrol('Style','edit','String',' 20','HorizontalAlignment','right','Fontsize',2*basicfsz,...
    'Units','normalized','FontName',sty,'Position',[yv(1)+yv(3)+0.01, yv(2), .03, basichi]);

yv = get(Plotparameter(2),'Position');
Plotsettings(2) = uicontrol('Style','edit','String',' 0.1','HorizontalAlignment','right','Fontsize',2*basicfsz,...
    'Units','normalized','FontName',sty,'Position',[yv(1)+yv(3)+0.01, yv(2), .03, basichi]);

yv = get(Plotparameter(3),'Position');
Plotsettings(3) = uicontrol('Style','edit','String',' 0.1','HorizontalAlignment','right','Fontsize',2*basicfsz,...
    'Units','normalized','FontName',sty,'Position',[yv(1)+yv(3)+0.01, yv(2), .03, basichi]);


% The AFS-transformation-information text-fields
xv = get(Plotparameter(1),'Position');
yv = get(Plotsettings(1),'Position');
SOLinfo(1) = uicontrol('Style','text','String',' used points: ','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[xv(1), xv(2)-0.2, xv(3)+yv(3)+0.01, basichi], 'Visible', 'off');

% The Expert mode text-fields
Experttext = uicontrol('Style','text','String','Lock-Mode','HorizontalAlignment','center','Fontsize',2*basicfsz,'FontWeight','bold',...
    'BackgroundColor',bgc,'Units','normalized','FontName',sty,'Position',[xv(1), yv(2)-0.275, xv(3)+yv(3)+0.01, basichi]);

% The Radio-buttons for polygon inflation or inverse polygon inflation
yv = get(Polyparameter(5),'Position');
Radiogroup(2) = uibuttongroup('visible','on','Position',[0.01 yv(2)-.1 .15 .09],'BackgroundColor',bgc);
upol = uicontrol('Style','radiobutton','String',' Polygon inflation','Units','normalized','Position',[0.01, 0.6, 0.98, 0.25],...
    'Fontsize',2*basicfsz,'FontWeight','bold','FontName',sty,'parent',Radiogroup(2),'HandleVisibility','off','Value',1,'BackgroundColor',bgc,...
    'TooltipString', sprintf('Use the classic "Polygon inflation" algorithm to compute\nan AFS with clearly separated segments.'));

uinv = uicontrol('Style','radiobutton','String',' Inverse polygon infl.','Units','normalized','Position',[0.01, 0.15, 0.98, 0.25],...
    'Fontsize',2*basicfsz,'FontWeight','bold','FontName',sty,'parent',Radiogroup(2),'HandleVisibility','off','BackgroundColor',bgc,...
    'TooltipString', sprintf('Use the "Inverse polygon inflation" algorithm to compute\nan AFS with isolated segments in close neighbourhood\nor if the AFS is only one segment.'));

% The Radio-buttons for AFS-motion-transformation
yv = get(SOLinfo(1),'Position');
Radiogroup(3) = uibuttongroup('visible','on','Position',[yv(1), yv(2)-.035, yv(3), .06],'BackgroundColor',bgc);
usmn = uicontrol('Style','radiobutton','String',' live view on','Units','normalized','Position',[0.01, 0.6, 0.98, 0.25],...
    'Fontsize',2*basicfsz,'FontWeight','bold','FontName',sty,'parent',Radiogroup(3),'HandleVisibility','off','Value',0,'BackgroundColor',bgc,...
    'TooltipString', sprintf('Activate live view mode to represent\nthe factors just in time and lock points.\nMove the mouse pointer through the AFS.'));

usmf = uicontrol('Style','radiobutton','String',' live view off','Units','normalized','Position',[0.01, 0.15, 0.98, 0.25],...
    'Fontsize',2*basicfsz,'FontWeight','bold','FontName',sty,'parent',Radiogroup(3),'HandleVisibility','off','Value',1,'BackgroundColor',bgc,...
    'TooltipString', sprintf('Deactivate live view mode.'));


% The axes for the data, the NFM, the AFS and the transformed AFS
AxesD = axes('Fontsize',2*basicfsz,'Units','normalized','FontName',sty,'Position',[0.15, 0.57, 0.35, 0.35]);
plot(AxesD,1,0,'w');

AxesNMF = axes('Fontsize',2*basicfsz,'Units','normalized','FontName',sty,'Position',[0.65, 0.57, 0.3, 0.35]);
plot(AxesNMF,1,0,'w');

AxesAFS = axes('Fontsize',2*basicfsz,'Units','normalized','FontName',sty,'Position',[0.2, 0.07, 0.3, 0.4]);
plot(AxesAFS,0,0,'w');

AxesSOL = axes('Fontsize',2*basicfsz,'Units','normalized','FontName',sty,'Position',[0.65, 0.07, 0.3, 0.4]);
plot(AxesSOL,1,0,'w');


% The buttons with all UserData in emptybutton
emptybutton = uicontrol('Style','pushbutton','Position',[0.02, 0.9, 0.1, basichi],'Visible','off','Callback',{@emptybutton_CB});

data.ismatrix = 0;
data.isnmf = 0;
data.isafs = 0;
data.calcp = calcp;
data.isExpert1 = 0;
data.isExpert2 = 0;
data.isLastClickLockMode = 0;
data.isimport = 0;
data.import = [];
set(emptybutton,'UserData',data);

xv = get(Plotparameter(1),'Position');
yv = get(Plotsettings(1),'Position');

Button(20) = uicontrol('Style','pushbutton','UserData',0,'String','Export to library','HorizontalAlignment','center','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[xv(1), xv(2)-.44, xv(3)+yv(3)+0.01, basichi],'Enable','off',...
    'TooltipString', 'Select and export a locked spectrum to the FAC-PACK library.');

Button(19) = uicontrol('Style','pushbutton','UserData',0,'String','View log file','HorizontalAlignment','center','Fontsize',1.5*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.01, yv(2)-.38, 0.15, basichi],'Enable','off',...
    'TooltipString', 'View log file for computation details.',...
    'Callback', {@buttonviewlogfile, fig, emptybutton, basichi, basicfsz, sty});

Button(12) = uicontrol('Style','pushbutton','UserData',0,'String','Save all','HorizontalAlignment','center','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[xv(1), xv(2)-.41, xv(3)+yv(3)+0.01, basichi],'Enable','on',...
    'TooltipString', 'Save all results in .mat-file.',...
    'Callback', {@buttonsaveall, emptybutton, Footnote, AxesAFS, AxesSOL, Plotsettings});

Button(18) = uicontrol('Style','pushbutton','UserData',0,'String','Reset','HorizontalAlignment','center','Fontsize',1.5*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.11, yv(2)-.41, 0.05, basichi],'Enable','off',...
    'TooltipString', 'Reset imported spectra.',...
    'Callback', {@buttonreimport, fig, emptybutton, Button, Footnote, AxesAFS, AxesSOL, lcs, LCS, 0});

Button(17) = uicontrol('Style','pushbutton','UserData',0,'String','Import spectra','HorizontalAlignment','center','Fontsize',1.5*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.01, yv(2)-.41, 0.0985, basichi],'Enable','off',...
    'TooltipString', sprintf('Import pure component spectra matrix "A" (no. spectra x channels)\nfrom .mat-file. Only for 3-component systems.'),...
    'Callback', {@buttonloadspectra, fig, emptybutton, Button, Footnote, AxesAFS, AxesSOL, basicfsz, basichi, calclib, lcs, LCS, sty});

Button(11) = uicontrol('Style','pushbutton','UserData',0,'String','Save AFS','HorizontalAlignment','center','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[xv(1), xv(2)-.38, xv(3)+yv(3)+0.01, basichi],'Enable','on',...
    'Tooltip', 'Save AFS-results (initial NMF + AFS) in .mat-file.',...
    'Callback', {@buttonsaveAFS, emptybutton, Button, Footnote, AxesAFS, AxesSOL, Plotsettings});

Button(16) = uicontrol('Style','pushbutton','UserData',2,'String','esc','HorizontalAlignment','center','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[yv(1), xv(2)-.335, yv(3), basichi],'Enable','on',...
    'Tooltip', 'Remove only the second locked point and its AFS.');

Button(15) = uicontrol('Style','pushbutton','UserData',1,'String','esc','HorizontalAlignment','center','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[yv(1), xv(2)-.305, yv(3), basichi],'Enable','on',...
    'Tooltip', 'Remove locked point(s) and reduced AFS.');

Button(14) = uicontrol('Style','pushbutton','UserData',2,'String','no. 2','HorizontalAlignment','center','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[xv(1), xv(2)-.335, xv(3), basichi],'Enable','on',...
    'Tooltip', 'Compute doubly-reduced AFS after locking two points.',...
    'Callback', {@buttonlockedAFS, fig, emptybutton, Button, Footnote, AxesAFS, AxesSOL, Polysettings, Plotsettings, lcs, LCS});

Button(13) = uicontrol('Style','pushbutton','UserData',1,'String','no. 1','HorizontalAlignment','center','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[xv(1), xv(2)-.305, xv(3), basichi],'Enable','on',...
    'Tooltip', 'Compute reduced AFS after locking a point.',...
    'Callback', {@buttonlockedAFS, fig, emptybutton, Button, Footnote, AxesAFS, AxesSOL, Polysettings, Plotsettings, lcs, LCS});

Button(10) = uicontrol('Style','pushbutton','UserData',3,'String','AFS 3','HorizontalAlignment','center','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[xv(1), xv(2)-.16, xv(3)+yv(3)+0.01, basichi],'Enable','on',...
    'Tooltip', 'Show the factors for segment 3 of the AFS (red).',...
    'Callback', {@buttontransformAFS, fig, emptybutton, Button, Footnote, AxesAFS, AxesSOL, Plotsettings, SOLinfo, lcs, LCS, usmn, usmf});

Button(9) = uicontrol('Style','pushbutton','UserData',2,'String','AFS 2','HorizontalAlignment','center','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[xv(1), xv(2)-.13, xv(3)+yv(3)+0.01, basichi],'Enable','on',...
    'Tooltip', 'Show the factors for segment 2 of the AFS (green).',...
    'Callback', {@buttontransformAFS, fig, emptybutton, Button, Footnote, AxesAFS, AxesSOL, Plotsettings, SOLinfo, lcs, LCS, usmn, usmf});

Button(8) = uicontrol('Style','pushbutton','UserData',1,'String','AFS 1','HorizontalAlignment','center','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[xv(1), xv(2)-.1, xv(3)+yv(3)+0.01, basichi],'Enable','on',...
    'Tooltip', 'Show the factors for segment 1 of the AFS (blue or black).',...
    'Callback', {@buttontransformAFS, fig, emptybutton, Button, Footnote, AxesAFS, AxesSOL, Plotsettings, SOLinfo, lcs, LCS, usmn, usmf});

yv = get(Polyparameter(5),'Position');
Button(7) = uicontrol('Style','pushbutton','UserData',0,'String','Compute AFS','HorizontalAlignment','center','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.01, yv(2)-.1325, 0.15, basichi],'Enable','on',...
    'Tooltip', 'Compute the AFS.',...
    'Callback', {@buttonAFS, fig, emptybutton, Button, Footnote, upol, uinv, Polysettings, AxesAFS, AxesSOL, AFSinfo, lcs, LCS, usmn, usmf});

Button(6) = uicontrol('Style','pushbutton','UserData',1,'String','plot S','HorizontalAlignment','center','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.52, 0.71, 0.1, basichi],'Enable','on',...
    'Tooltip', 'Show initial factor A.',...
    'Callback', {@buttonplotNMF, emptybutton, Button, Footnote, AxesNMF, NMFinfo, lcs});

Button(5) = uicontrol('Style','pushbutton','UserData',0,'String','plot C','HorizontalAlignment','center','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.52, 0.74, 0.1, basichi],'Enable','on',...
    'Tooltip', 'Show initial factor C.',...
    'Callback', {@buttonplotNMF, emptybutton, Button, Footnote, AxesNMF, NMFinfo, lcs});

Button(4) = uicontrol('Style','pushbutton','UserData',2,'String','plot C and S','HorizontalAlignment','center','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.52, 0.77, 0.1, basichi],'Enable','on',...
    'Tooltip', 'Show initial factor C and A.',...
    'Callback', {@buttonplotNMF, emptybutton, Button, Footnote, AxesNMF, NMFinfo, lcs});

Button(3) = uicontrol('Style','pushbutton','UserData',0,'String','Initial NMF','HorizontalAlignment','center','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.52, 0.8, 0.1, basichi],'Enable','on',...
    'Tooltip', 'Compute an initial NMF.',...
    'Callback', {@buttoninitialNMF, emptybutton, Button, Footnote, AxesNMF, AxesAFS, AxesSOL, NMFparameter, NMFinfo, Polysettings, lcs, usmn, usmf});

% The Radio-buttons for two or three component systems
Radiogroup(1) = uibuttongroup('visible','on','Position',[0.52, 0.86, .1, .06],'BackgroundColor',bgc);
    
utwo = uicontrol('Style','radiobutton','String',' 2 components','Units','normalized','Position',[0.01, 0.6, 0.98, 0.25],...
    'Fontsize',2*basicfsz,'FontWeight','bold','FontName',sty,'parent',Radiogroup(1),'HandleVisibility','off','Value',0,'BackgroundColor',bgc,...
    'TooltipString', sprintf('Compute an NMF for 2 components,\nsee singular values.'));

uthr = uicontrol('Style','radiobutton','String',' 3 components','Units','normalized','Position',[0.01, 0.15, 0.98, 0.25],...
    'Fontsize',2*basicfsz,'FontWeight','bold','FontName',sty,'parent',Radiogroup(1),'HandleVisibility','off','Value',0,'BackgroundColor',bgc,...
    'TooltipString', sprintf('Compute an NMF for 3 components,\nsee singular values.'));

set(utwo,'Value',0);
set(uthr,'Value',0);
set(Radiogroup(1),'SelectionChangeFcn', {@buttonselect23, emptybutton, Button, AxesNMF, AxesAFS, AxesSOL, NMFparameter, utwo, uthr, usmn, usmf, Polysettings});

Button(2) = uicontrol('Style','pushbutton','UserData',0,'String','Transpose','HorizontalAlignment','center','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.02, 0.87, 0.1, basichi],'Enable','on',...
    'TooltipString', 'Transpose the data matrix.',...
    'Callback', {@buttontranspose, emptybutton, Button, Footnote, AxesD, AxesNMF, AxesAFS, AxesSOL, Datainfo, SVDinfo, Polysettings, utwo, uthr, usmn, usmf});

Button(1) = uicontrol('Style','pushbutton','UserData',0,'String','Load','HorizontalAlignment','center','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',get(emptybutton,'Position'),...
    'TooltipString', sprintf('Load mat-file containing data matrix "D" (spectra  x channels)\nand channels-vector "x".'),...
    'Callback',{@buttonload, emptybutton, Button, Footnote, AxesD, AxesNMF, AxesAFS, AxesSOL, Datainfo,...
    SVDinfo, Polysettings, utwo, uthr, usmn, usmf, titles});

set(Radiogroup(3),'SelectionChangeFcn', {@buttonselectlv, emptybutton, AxesSOL, usmn, usmf});

set(Button(15),'Callback', {@buttonesclockedAFS, fig, emptybutton, Button, AxesAFS, AxesSOL, lcs, LCS});
set(Button(16),'Callback', {@buttonesclockedAFS, fig, emptybutton, Button, AxesAFS, AxesSOL, lcs, LCS});

set(Button(20),'Callback', {@buttonexport2libAFS, emptybutton, Button, lcs, basichi, basicfsz, sty, calclib, version});

onoffButton(Button,1,2:16);

set(gcf,'WindowButtonMotionFcn', {@liveview, emptybutton, Button, Helpfield, AxesD, AxesNMF, AxesAFS, AxesSOL, Radiogroup, usmn, lcs, basichi})

set(gcf,'WindowButtonDownFcn', {@extractaxes, fig, emptybutton, Button, AxesD, AxesNMF, AxesAFS, AxesSOL, lcs, LCS, usmn, usmf})

set(fig,'Visible','on')

function emptybutton_CB(hObject, eventdata)
data.ismatrix = 0;
data.isnmf = 0;
data.isafs = 0;
set(hObject,'UserData',data);
