function subGUI4EDP(version,nr,basicfsz)

scrsz = get(0,'ScreenSize');
fig = figure(nr);
pause(.1);
set(fig,'Position',[1/6*scrsz(3:4) 4/5*scrsz(3) 1/2*scrsz(4)]);
set(fig,'Name',['FACPACK (Computation of essential information, version ',num2str(version,'%2.2f'), ')'],...
    'NumberTitle','off','MenuBar','none','Toolbar','none','color','white','Visible','off');

if ispc
%     basicfsz = 5;
    basichi = 0.05;
else
%     basicfsz = 6.5;
    basichi = 0.05;
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','D','HorizontalAlignment','center','Fontsize',3*basicfsz,'BackgroundColor','w',...
    'Units','normalized','FontName',sty,'Position',[0.19, 0.89, 0.25, basichi]);

titles(2) = uicontrol('Style','text','String','U-space','HorizontalAlignment','center','Fontsize',3*basicfsz,'BackgroundColor','w',...
    'Units','normalized','FontName',sty,'Position',[0.46, 0.89, 0.25, basichi]);

titles(3) = uicontrol('Style','text','String','V-space','HorizontalAlignment','center','Fontsize',3*basicfsz,'BackgroundColor','w',...
    'Units','normalized','FontName',sty,'Position',[0.73, 0.89, 0.25, basichi]);

options(1) = uicontrol('Style','text','String','Noise level:','HorizontalAlignment','center','Fontsize',2*basicfsz,'BackgroundColor','w',...
    'Units','normalized','FontName',sty,'Position',[0.005, 0.54, 0.09, basichi]);

options(2) = uicontrol('Style','edit','String',' 0.02','HorizontalAlignment','right','Fontsize',2*basicfsz,...
    'Units','normalized','FontName',sty,'Position',[0.08, 0.54, 0.08, basichi]);

optionsray(1) = uicontrol('Style','text','String','No. rays:','HorizontalAlignment','center','Fontsize',2*basicfsz,'BackgroundColor','w',...
    'Units','normalized','FontName',sty,'Position',[0.005, 0.47, 0.09, basichi]);

optionsray(2) = uicontrol('Style','edit','String',' 500','HorizontalAlignment','right','Fontsize',2*basicfsz,...
    'Units','normalized','FontName',sty,'Position',[0.08, 0.47, 0.08, basichi]);

% The axes for the data, the NFM, the AFS and the transformed AFS

AxesD1 = axes('Fontsize',2*basicfsz,'Units','normalized','FontName',sty,'Position',[0.19, 0.1, 0.25, 0.34]);
plot(AxesD1,1,0,'w');

AxesD2 = axes('Fontsize',2*basicfsz,'Units','normalized','FontName',sty,'Position',[0.19, 0.51, 0.25, 0.34]);
plot(AxesD2,1,0,'w');

AxesU = axes('Fontsize',2*basicfsz,'Units','normalized','FontName',sty,'Position',[0.46, 0.1, 0.25, 0.75]);
plot(AxesU,1,0,'w');

AxesV = axes('Fontsize',2*basicfsz,'Units','normalized','FontName',sty,'Position',[0.73, 0.1, 0.25, 0.75]);
plot(AxesV,1,0,'w');


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


Button(3) = uicontrol('Style','pushbutton','UserData',0,'String','Save Results','HorizontalAlignment','center','Fontsize',2*basicfsz,...
    'Units','normalized','FontName',sty,'Position',[0.02, 0.34, 0.14, basichi],'Enable','off',...
    'Callback', {@buttonsave, emptybutton});

Button(2) = uicontrol('Style','pushbutton','UserData',0,'String','Compute polytopes','HorizontalAlignment','center','Fontsize',2*basicfsz,...
    'Units','normalized','FontName',sty,'Position',[0.02, 0.4, 0.14, basichi],'Enable','off',...
    'Callback', {@buttonEDP, emptybutton, Button, options, optionsray, AxesU, AxesV,AxesD1, AxesD2, lcs});

% The Radio-buttons for two or three component systems
Radiogroup(1) = uibuttongroup('visible','on','Position',[0.02, 0.62, .14, .17],'BackgroundColor',bgc);

usix = uicontrol('Style','radiobutton','String',' 6 components','Units','normalized','Position',[0.01, 0.1, 0.98, 0.2],...
    'Fontsize',2*basicfsz,'FontName',sty,'parent',Radiogroup(1),'HandleVisibility','off','Value',0,'BackgroundColor',bgc);

ufiv= uicontrol('Style','radiobutton','String',' 5 components','Units','normalized','Position',[0.01, 0.3, 0.98, 0.2],...
    'Fontsize',2*basicfsz,'FontName',sty,'parent',Radiogroup(1),'HandleVisibility','off','Value',0,'BackgroundColor',bgc);

ufou = uicontrol('Style','radiobutton','String',' 4 components','Units','normalized','Position',[0.01, 0.5, 0.98, 0.2],...
    'Fontsize',2*basicfsz,'FontName',sty,'parent',Radiogroup(1),'HandleVisibility','off','Value',0,'BackgroundColor',bgc);

uthr = uicontrol('Style','radiobutton','String',' 3 components','Units','normalized','Position',[0.01, 0.7, 0.98, 0.2],...
    'Fontsize',2*basicfsz,'FontName',sty,'parent',Radiogroup(1),'HandleVisibility','off','Value',0,'BackgroundColor',bgc);

set(usix,'Value',0);
set(ufiv,'Value',0);
set(ufou,'Value',0);
set(uthr,'Value',0);
set(Radiogroup(1),'SelectionChangeFcn', {@buttonselect34, emptybutton, Button, AxesU, AxesV, AxesD1,AxesD2, ufou, uthr, ufiv, usix,optionsray});


Radiogroup(2) = uibuttongroup('visible','on','Position',[0.02, 0.2, .14, .13],'BackgroundColor',bgc);

uline = uicontrol('Style','radiobutton','String',' marked spectra','Units','normalized','Position',[0.01, 0.7, 0.98, 0.3],...
    'Fontsize',2*basicfsz,'FontName',sty,'parent',Radiogroup(2),'HandleVisibility','off','Value',0,'BackgroundColor',bgc);
uspec = uicontrol('Style','radiobutton','String',' selected spectra','Units','normalized','Position',[0.01, 0.4, 0.98, 0.3],...
    'Fontsize',2*basicfsz,'FontName',sty,'parent',Radiogroup(2),'HandleVisibility','off','Value',0,'BackgroundColor',bgc);
uboth = uicontrol('Style','radiobutton','String',' both','Units','normalized','Position',[0.01, 0.1, 0.98, 0.3],...
    'Fontsize',2*basicfsz,'FontName',sty,'parent',Radiogroup(2),'HandleVisibility','off','Value',0,'BackgroundColor',bgc);

set(uline,'Value',1)
set(uspec,'Value',0)
set(uboth,'Value',0)
set(Radiogroup(2),'SelectionChangeFcn', {@buttonselectplot, emptybutton, AxesD1, AxesD2, lcs, uline, uspec, uboth});


%
% unoi = uicontrol('Style','radiobutton','String',' with noise','Units','normalized','Position',[0.01, 0.15, 0.98, 0.25],...
%     'Fontsize',2*basicfsz,'FontName',sty,'parent',Radiogroup(2),'HandleVisibility','off','Value',0,'BackgroundColor',bgc);
%
% umod = uicontrol('Style','radiobutton','String',' without noise','Units','normalized','Position',[0.01, 0.6, 0.98, 0.25],...
%     'Fontsize',2*basicfsz,'FontName',sty,'parent',Radiogroup(2),'HandleVisibility','off','Value',0,'BackgroundColor',bgc);
%
% set(umod,'Value',0);
% set(unoi,'Value',0);
% set(Radiogroup(2),'SelectionChangeFcn', {@buttonselectnoise, emptybutton, Button, AxesU, AxesV, umod, unoi});
%
% set(Button(2),'Callback', {@buttonEDP, emptybutton, Button, options, AxesU, AxesV, lcs});

Button(1) = uicontrol('Style','pushbutton','UserData',0,'String','Load','HorizontalAlignment','center','Fontsize',2*basicfsz,...
    '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, AxesU, AxesV, AxesD1, AxesD2, ufou, uthr, ufiv, usix, uline, uspec, uboth});

set(gcf,'WindowButtonDownFcn', {@fcnbd, fig, emptybutton, Button, AxesU, AxesV})

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

function emptybutton_CB(hObject, eventdata)
data.ismatrix = 0;
data.s = [];
data.noise = [];
set(hObject,'UserData',data);

function buttonEDP(hObject, eventdata, Empty_Button, Buttons, options, optionsray, AxesU, AxesV, AxesD1, AxesD2, lcs)
data = get(Empty_Button,'UserData');

if data.s == 3

    optionsray(2).String=max(10, min(2000, str2num(optionsray(2).String)));

    [FPC, FPS, essU, essV] = fpess4noise(data.V, data.U*data.S, 3, 1.E-5, -str2num(get(options(2),'String')), str2num(optionsray(2).String));
    FPC = FPC([1:end 1],:);
    FPS = FPS([1:end 1],:);
    IPS = [];
    for i=1:size(FPC,1)-1
        if min(FPC(i,:) == [0 0])<1 && min(FPC(i+1,:) == [0 0])<1
            IPS(end+1,:) = (FPC(i:i+1,:)\[-1;-1])';
        end
    end
    if size(IPS,1)>1
        K = convhull(IPS(:,1),IPS(:,2));
        IPS = IPS(K,:);
    end

    IPC = [];
    for i=1:size(FPS,1)-1
        if min(FPS(i,:) == [0 0])<1 && min(FPS(i+1,:) == [0 0])<1
            IPC(end+1,:) = (FPS(i:i+1,:)\[-1;-1])';
        end
    end
    if size(IPC,1)>1
        K = convhull(IPC(:,1),IPC(:,2));
        IPC = IPC(K,:);
    end
elseif data.s == 4

    optionsray(2).String=num2str(max(2*10^2, min(2*500^2, str2num(optionsray(2).String)))); %2*2000^2

    [FPC, FPS, essU, essV] = fpess4noise(data.V, data.U*data.S, 4, 1.E-4, -str2num(get(options(2),'String')), str2num(optionsray(2).String));

    kfpc = convhull(FPC);
    kfps = convhull(FPS);

    IPC = [];
    for i=1:size(kfps,1)
        IPC(end+1,:) = (FPS(kfps(i,:),:)\[-1;-1;-1])';
    end

    IPS = [];
    for i=1:size(kfpc,1)
        IPS(end+1,:) = (FPC(kfpc(i,:),:)\[-1;-1;-1])';
    end

elseif data.s == 5

    optionsray(2).String=num2str(max(2*10^3, min(2*500^3, str2num(optionsray(2).String))));

    [FPC, FPS, essU, essV] = fpess4noise(data.V, data.U*data.S, 5, 1.E-4, -str2num(get(options(2),'String')), str2num(optionsray(2).String));

    kfpc = convhulln(FPC);
    kfps = convhulln(FPS);

    IPC = [];
    for i=1:size(kfps,1)
        IPC(end+1,:) = (FPS(kfps(i,:),:)\[-1;-1;-1;-1])';
    end

    IPS = [];
    for i=1:size(kfpc,1)
        IPS(end+1,:) = (FPC(kfpc(i,:),:)\[-1;-1;-1;-1])';
    end
elseif data.s == 6

    optionsray(2).String=num2str(max(2*10^4, min(2*500^4, str2num(optionsray(2).String))));

    [FPC, FPS, essU, essV] = fpess4noise(data.V, data.U*data.S, 6, 1.E-4, -str2num(get(options(2),'String')), str2num(optionsray(2).String));

    kfpc = convhulln(FPC);
    kfps = convhulln(FPS);

    IPC = [];
    for i=1:size(kfps,1)
        IPC(end+1,:) = (FPS(kfps(i,:),:)\[-1;-1;-1;-1;-1])';
    end

    IPS = [];
    for i=1:size(kfpc,1)
        IPS(end+1,:) = (FPC(kfpc(i,:),:)\[-1;-1;-1;-1;-1])';
    end
end


data.FPS = FPS;
data.FPC = FPC;
data.IPS = IPS;
data.IPC = IPC;
data.essU = essU;
data.essV = essV;

onoffButton(Buttons,3,[]);
if data.s == 3
    plotIFP3(FPS, IPS, FPC, IPC, lcs, AxesU, AxesV, essU, essV,data)
elseif data.s == 4
    plotIFP4(FPS, IPS, FPC, IPC, lcs, AxesU, AxesV,data)
end

if data.plt==1
        plot(AxesD1, data.D,'k');
        axis(AxesD1,'tight')
        plot(AxesD2, data.D','k');
        axis(AxesD2,'tight')

        hold(AxesD1,'on')
        xline(AxesD1,data.essU,'Color',lcs(2,:))
        hold(AxesD1,'off')
        hold(AxesD2,'on')
        xline(AxesD2,data.essV,'Color',lcs(2,:))
        hold(AxesD2,'off')

    elseif data.plt==2
        cla(AxesD1)
        cla(AxesD2)
        hold(AxesD2,'on')
        for j=1:length(data.essU)
            plot(AxesD2, 1:size(data.D,2),data.D(data.essU(j),:),'k');
        end
        axis(AxesD2,'tight')
        hold(AxesD2,'off')
        hold(AxesD1,'on')
        for j=1:length(data.essV)
            plot(AxesD1, 1:size(data.D,1), (data.D(:,data.essV(j)))','k');
        end
        axis(AxesD1,'tight')
        hold(AxesD1,'off')
    elseif data.plt ==3
        cla(AxesD1)
        cla(AxesD2)
        hold(AxesD2,'on')
        for j=1:length(data.essU)
            plot(AxesD2, 1:size(data.D,2),data.D(data.essU(j),:),'k');
        end
        axis(AxesD2,'tight')
        hold(AxesD2,'off')
        hold(AxesD1,'on')
        for j=1:length(data.essV)
            plot(AxesD1, 1:size(data.D,1), (data.D(:,data.essV(j)))','k');
        end
        axis(AxesD1,'tight')
        hold(AxesD1,'off')

        hold(AxesD1,'on')
        xline(AxesD1,data.essU,'Color',lcs(2,:))
        hold(AxesD1,'off')
        hold(AxesD2,'on')
        xline(AxesD2,data.essV,'Color',lcs(2,:))
        hold(AxesD2,'off')
    end


set(Empty_Button,'UserData',data);

function plotIFP3(FPS, IPS, FPC, IPC, lcs, AxesU, AxesV, essU, essV,data)

hold(AxesV,'on')
yls = [min(FPS(:,2)) max(FPS(:,2))];
ylin = 5*yls;
for i=1:1:size(data.D,2)
    xlin(i,:) = -data.V(i,1)/data.V(i,2)-ylin*data.V(i,3)/data.V(i,2);
    if ismember(i,essV)
        colo=lcs(2,:);
    else
        colo=0.7*[1 1 1];
    end
    if i==1
        plot(AxesV,xlin(i,:), ylin,'color',colo);
    else
        line(AxesV,xlin(i,:), ylin,'color',colo);
    end
end
%     line(FPSo(:,1), FPSo(:,2),'color',[0.1 0.1 0.1], 'linewidth', 1)


a = diag(1./data.U(:,1)/data.S(1,1))*data.U(:,2:3)*data.S(2:3,2:3);
for i=1:size(a,1)
    if ismember(i,essU)
        colo=lcs(2,:);
    else
        colo=0.7*[1 1 1];
    end
    plot(AxesV,a(i,1),a(i,2),'o','color',colo)
end


p1=plot(AxesV,FPS(:,1), FPS(:,2),'color',lcs(1,:), 'linewidth', 1);
% hold(AxesV,'on')
if size(IPS,1)>1
    xls = [min(min(FPS(:,1)),min(IPS(:,1))) max(max(FPS(:,1)),max(IPS(:,1)))];
    yls = [min(min(FPS(:,2)),min(IPS(:,2))) max(max(FPS(:,2)),max(IPS(:,2)))];
    p2=plot(AxesV,IPS(:,1), IPS(:,2),'color',lcs(5,:), 'linewidth', 1);
    l = legend(AxesV,[p1,p2],'Outer Polygon','Inner Polygon');
    set(l,'Fontsize',15);
else
    xls = [min(FPS(:,1)) max(FPS(:,1))];
    yls = [min(FPS(:,2)) max(FPS(:,2))];
    l = legend(AxesV,p1,'Outer Polygon');
    set(l,'Fontsize',15);
end
hold(AxesV,'off')
set(AxesV,'xlim',1.05*xls,'ylim',1.05*yls,'Fontsize',15)

hold(AxesU,'on')
ylc = [min(FPC(:,2)) max(FPC(:,2))];
ylin = 5*ylc;
for i=1:1:size(data.D,1)
    xlin(i,:) = -data.U(i,1)*data.S(1,1)/data.U(i,2)/data.S(2,2)-ylin*data.U(i,3)*data.S(3,3)/data.U(i,2)/data.S(2,2);
    if ismember(i,essU)
        colo=lcs(2,:);
    else
        colo=0.7*[1 1 1];
    end
    if i==1
        plot(AxesU,xlin(i,:), ylin,'color',colo);
    else
        line(AxesU,xlin(i,:), ylin,'color', colo);
    end
end

b = diag(1./data.V(:,1))*data.V(:,2:3);
for i=1:size(b,1)
    if ismember(i,essV)
        colo=lcs(2,:);
    else
        colo=0.7*[1 1 1];
    end
    plot(AxesU,b(i,1),b(i,2),'o','color',colo)
end

p1=plot(AxesU,FPC(:,1), FPC(:,2),'color',lcs(1,:), 'linewidth', 1);
% hold(AxesU,'on')
if size(IPC,1)>1
    xlc = [min(min(FPC(:,1)),min(IPC(:,1))) max(max(FPC(:,1)),max(IPC(:,1)))];
    ylc = [min(min(FPC(:,2)),min(IPC(:,2))) max(max(FPC(:,2)),max(IPC(:,2)))];
    p2=plot(AxesU,IPC(:,1), IPC(:,2),'color',lcs(5,:), 'linewidth', 1);
    l = legend(AxesU,[p1 p2],'Outer Polygon','Inner Polygon');
else
    xlc = [min(FPC(:,1)) max(FPC(:,1))];
    ylc = [min(FPC(:,2)) max(FPC(:,2))];
    l = legend(AxesU,[p1],'Outer Polygon');
end
hold(AxesU,'off')
set(AxesU,'xlim',1.05*xlc,'ylim',1.05*ylc,'Fontsize',15)
set(l,'Fontsize',15);


function plotIFP4(FPS, IPS, FPC, IPC, lcs, AxesU, AxesV,data)
xls = [min(FPS(:,1)) max(FPS(:,1))];
yls = [min(FPS(:,2)) max(FPS(:,2))];
zls = [min(FPS(:,3)) max(FPS(:,3))];

kips = convhull(IPS);
kipc = convhull(IPC);

kfpc = convhull(FPC);
kfps = convhull(FPS);

trisurf(kfps, FPS(:,1), FPS(:,2), FPS(:,3), 'facecolor', lcs(1,:), 'facealpha',0.2,'Parent',AxesV);
hold(AxesV,'on');
trisurf(kips, IPS(:,1), IPS(:,2), IPS(:,3), 'facecolor', lcs(2,:), 'facealpha',0.7,'Parent',AxesV);
hold(AxesV,'off');
l = legend(AxesV,'Outer Polytope','Inner Polytope');
set(AxesV,'xlim',1.05*xls,'ylim',1.05*yls,'zlim',1.05*zls,'Fontsize',15)

xlc = [min(FPC(:,1)) max(FPC(:,1))];
ylc = [min(FPC(:,2)) max(FPC(:,2))];
zlc = [min(FPC(:,3)) max(FPC(:,3))];
trisurf(kfpc, FPC(:,1), FPC(:,2), FPC(:,3), 'facecolor', lcs(1,:), 'facealpha',0.2,'Parent',AxesU);
hold(AxesU,'on');
trisurf(kipc, IPC(:,1), IPC(:,2), IPC(:,3), 'facecolor', lcs(2,:), 'facealpha',0.7,'Parent',AxesU);
hold(AxesU,'off');
l = legend(AxesU,'Outer Polytope','Inner Polytope');
set(AxesU,'xlim',1.05*xlc,'ylim',1.05*ylc,'zlim',1.05*zlc,'Fontsize',15)


function buttonload(hObject, eventdata, Empty_Button, Buttons, AxesU, AxesV,AxesD1, AxesD2, ufou, uthr, ufiv, usix,uline,uspec,uboth)
data = get(Empty_Button,'UserData');
data.ismatrix = 0;
set(ufou,'Value',0);
set(uthr,'Value',0);
set(ufiv,'Value',0);
set(usix,'Value',0);
set(uline,'Value',1);
set(uspec,'Value',0);
set(uboth,'Value',0);
onoffButton(Buttons,[],2:3);

data.plt=1;

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

if isfield(data,'PathName') == 0
    actpwd = pwd;
    if strcmp(actpwd(end-9:end), 'subfcnsAFS') == 1
        data.PathName = actpwd(1:end-10);
    else
        data.PathName = [pwd, dlm];
    end
end

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

if FilterIndex == 0
    set(hObject,'UserData',data);

    plot(AxesU,1,0,'w');
    plot(AxesV,1,0,'w');
    set(Empty_Button,'UserData',data);
    return
end
data.FileName = FileName;
data.PathName = PathName;

load([PathName FileName]);
if ~exist('D')
    set(Empty_Button,'UserData',data);
    return
else
    k = size(D,1);
    n = size(D,2);
    if min(k,n) < 2
        onoffButton(Buttons,[],2:3);
        set(Empty_Button,'UserData',data);
        return
    end

    if exist('t')
        data.t = t;
    elseif (~exist('t')) && (exist('T')) && (max(size(T)) == k) && (min(size(T)) == 1)
        data.t = T;
    elseif (~exist('t')) || (max(size(t)) ~= k) || (min(size(t)) ~= 1)
        data.t = 0:k-1;
    end

    if exist('x')
        data.x = x;
    elseif (~exist('x')) && (exist('X')) && (max(size(X)) == n) && (min(size(X)) == 1)
        data.x = X;
    elseif (~exist('x')) || (max(size(x)) ~= n) || (min(size(x)) ~= 1)
        data.x = 0:n-1;
    end

    [U,S,V] = svds(D,6);
    for i=1:size(U,2)
        if -min(V(:,i)) > max(V(:,i))
            U(:,i) = -U(:,i);
            V(:,i) = -V(:,i);
        end
    end

    data.U = U;
    data.S = S;
    data.V = V;
    data.D = D;
    data.ismatrix = 1;

    onoffButton(Buttons,[],2:3);
end

plot(AxesU,1,0,'w');
plot(AxesV,0,0,'w');
plot(AxesD1, D,'k');
axis(AxesD1,'tight')
plot(AxesD2, D','k');
axis(AxesD2,'tight')

set(Empty_Button,'UserData',data);


function buttonselect34(hObject, eventdata, Empty_Button, Buttons, AxesU, AxesV, AxesD1,AxesD2, ufou, uthr, ufiv, usix,optionsray)
data = get(Empty_Button,'UserData');

if get(uthr,'Value') == 1
    data.s = 3;
    optionsray(2).String=num2str(50);
elseif get(ufou,'Value') == 1
    data.s = 4;
    optionsray(2).String=num2str(2*50^2);
elseif get(ufiv,'Value') == 1
    data.s = 5;
    optionsray(2).String=num2str(2*50^3);
elseif get(usix,'Value') == 1
    data.s = 6;
    optionsray(2).String=num2str(2*50^4);
end

plot(AxesU,1,0,'w');
plot(AxesV,0,0,'w');
if (isfield(data,'ismatrix') == 1) && (data.ismatrix == 1)
    plot(AxesD1, data.D,'k');
    axis(AxesD1,'tight')
    plot(AxesD2, data.D','k');
    axis(AxesD2,'tight')
end

if (isfield(data,'ismatrix') == 1) && (data.ismatrix == 1) %&& (isfield(data,'noise') == 1) %&& (data.noise == 0 || data.noise == 1)
    onoffButton(Buttons,2,3);
end
set(Empty_Button,'UserData',data);
%%
function buttonselectplot(hObject, eventdata, Empty_Button, AxesD1, AxesD2, lcs, uline, uspec, uboth)
data = get(Empty_Button,'UserData');

if uline.Value
    data.plt=1;
elseif uspec.Value
    data.plt=2;
elseif uboth.Value
    data.plt=3;
end


if (isfield(data,'ismatrix') == 1) && (data.ismatrix == 1) && (isfield(data,'essU') == 1)
    if data.plt==1
        plot(AxesD1, data.D,'k');
        axis(AxesD1,'tight')
        plot(AxesD2, data.D','k');
        axis(AxesD2,'tight')

        hold(AxesD1,'on')
        xline(AxesD1,data.essU,'Color',lcs(2,:))
        hold(AxesD1,'off')
        hold(AxesD2,'on')
        xline(AxesD2,data.essV,'Color',lcs(2,:))
        hold(AxesD2,'off')

    elseif data.plt==2
        cla(AxesD1)
        cla(AxesD2)
        hold(AxesD2,'on')
        for j=1:length(data.essU)
            plot(AxesD2, 1:size(data.D,2),data.D(data.essU(j),:),'k');
        end
        axis(AxesD2,'tight')
        hold(AxesD2,'off')
        hold(AxesD1,'on')
        for j=1:length(data.essV)
            plot(AxesD1, 1:size(data.D,1), (data.D(:,data.essV(j)))','k');
        end
        axis(AxesD1,'tight')
        hold(AxesD1,'off')
    elseif data.plt ==3
        cla(AxesD1)
        cla(AxesD2)
        hold(AxesD2,'on')
        for j=1:length(data.essU)
            plot(AxesD2, 1:size(data.D,2),data.D(data.essU(j),:),'k');
        end
        axis(AxesD2,'tight')
        hold(AxesD2,'off')
        hold(AxesD1,'on')
        for j=1:length(data.essV)
            plot(AxesD1, 1:size(data.D,1), (data.D(:,data.essV(j)))','k');
        end
        axis(AxesD1,'tight')
        hold(AxesD1,'off')

        hold(AxesD1,'on')
        xline(AxesD1,data.essU,'Color',lcs(2,:))
        hold(AxesD1,'off')
        hold(AxesD2,'on')
        xline(AxesD2,data.essV,'Color',lcs(2,:))
        hold(AxesD2,'off')
    end



end
set(Empty_Button,'UserData',data);
%%
function fcnbd(hObject, eventdata, parentfig, Empty_Button, Buttons, AxesU, AxesV)
data = get(Empty_Button,'UserData');
SelTyp = get(gcbf, 'SelectionType');

if strcmp(SelTyp,'alt') == 1
    pt = get(hObject, 'CurrentPoint');
    sz = get(hObject, 'Position');
    x = pt(1,1:2)./sz(1,3:4);

    posU = get(AxesU, 'Position');
    posV = get(AxesV, 'Position');

    if (posU(1) < x(1)) && (x(1) < posU(1)+posU(3)) && (posU(2) < x(2)) && (x(2) < posU(2)+posU(4))
        tmpc = figure;
        copyobj(AxesU,tmpc);
        tmpa = get(tmpc,'CurrentAxes');
        set(tmpa,'position',[0.15 0.15 0.75 0.75]);
        title(tmpa, 'U-space');
        xlabel(tmpa, '$T_{1,2}$','interpreter','latex');
        ylabel(tmpa, '$T_{1,3}$','interpreter','latex');

    elseif (posV(1) < x(1)) && (x(1) < posV(1)+posV(3)) && (posV(2) < x(2)) && (x(2) < posV(2)+posV(4))
        tmpc = figure;
        copyobj(AxesV,tmpc);
        tmpa = get(tmpc,'CurrentAxes');
        set(tmpa,'position',[0.15 0.15 0.75 0.75]);
        title(tmpa, 'V-space');
        xlabel(tmpa, '$(T^{-1})_{1,2}$','interpreter','latex');
        ylabel(tmpa, '$(T^{-1})_{1,3}$','interpreter','latex');

    end
end

function buttonsave(hObject, eventdata, Empty_Button)
data = get(Empty_Button,'UserData');
if isfield(data,'FPS')*isfield(data,'FPC')*isfield(data,'IPS')*isfield(data,'IPC') == 0
    return;
end
U = data.U;
S = data.S;
V = data.V;
D = data.D;
if isfield(data,'x')
    x = data.x;
else
    x = 1:size(data.D,2);
end
if isfield(data,'t')
    t = data.t;
else
    t = 1:size(data.D,1);
end
FPS = data.FPS;
FPC = data.FPC;
IPS = data.IPS;
IPC = data.IPC;
essU = data.essU;
essV = data.essV;

uisave({'U','S','V','D','x','t','FPS','FPC','IPS','IPC','essU','essV'},[data.PathName,'FPIPess_',data.FileName]);

function onoffButton(handle, on, off)
for i=1:length(on)
    set(handle(on(i)),'Enable','on');
end
for i=1:length(off)
    set(handle(off(i)),'Enable','off');
end

