function disttool(action,flag);
%DISTTOOL Demonstration of many probability distributions.
%   DISTTOOL creates interactive plots of probability distributions.
%   This is a demo that displays a plot of the cdf or pdf of
%   the distributions in the Statistics Toolbox.
%
%   Use popup menus to change the distibution (Normal to Binomial) or
%   the function (cdf to pdf). 
%
%   You can change the parameters of the distribution by typing
%   a new value or by moving a slider.    
%
%   You can interactively calculate new values by dragging a reference 
%   line across the plot.
%
%   Call DISTTOOL without arguments.
   
%   B.A. Jones 3-15-93
%   Copyright (c) 1993-96 by The MathWorks, Inc.
%   $Revision: 2.5 $  $Date: 1996/03/18 14:00:58 $

if nargin < 1
    action = 'start';
end

%On recursive calls get all necessary handles and data.
if ~strcmp(action,'start')   
   
   f = get(gcf,'Userdata');

   p1_field     = f(1);
   p1_hi        = f(2);
   p1_lo        = f(3);
   p1_slider    = f(4);
   p2_field     = f(5);
   p2_hi        = f(6);
   p2_lo        = f(7);
   p2_slider    = f(8);
   p1_text      = f(9);
   p2_text      = f(10);
   yaxis_text   = f(11);
   popup        = f(12);
   functionpopup= f(13);
   x_field      = f(14);
   y_field      = f(15);
   distcontrol  = f(16);
   p1limits     = f(17);
   p2limits     = f(18);
   dist_axes    = f(19);
   p3_field     = f(20);
   p3_hi        = f(21);
   p3_lo        = f(22);
   p3_slider    = f(23);
   p3_text      = f(24);
   
   p1lim = get(p1limits,'Userdata');
   p2lim = get(p2limits,'Userdata');
   distribution = get(distcontrol,'Userdata');
   functiontype = get(functionpopup,'Value');
   newx = str2num(get(x_field,'string'));
   newy = str2num(get(y_field,'string'));
   parameter1 = str2num(get(p1_field,'String'));
   p1high = str2num(get(p1_hi,'String'));
   p1low = str2num(get(p1_lo,'String'));
   parameter2 = str2num(get(p2_field,'String'));
   p2high = str2num(get(p2_hi,'String'));
   p2low = str2num(get(p2_lo,'String'));
   parameter3 = str2num(get(p3_field,'String'));
   p3high = str2num(get(p3_hi,'String'));
   p3low = str2num(get(p3_lo,'String'));
   popupvalue = get(popup,'Value');

   h = get(dist_axes,'userdata');

   density_line = h(1);
   vwitnessline = h(2);
   hwitnessline = h(3);

end

if strcmp(action,'start'),


% Create intial parameter values 
parameter1  =   0; 
parameter2  =   1;
parameter3  =   1;
p1high      =   2;
p1low       =  -2;
p2high      =   2;
p2low       = 0.5;
p1lim       = [-Inf Inf];
p2lim       = [0 Inf];

distribution = 'Normal';
popupvalue   = 14;
functiontype = 1;
newx         = 0;
newy         = 0.5;
flag         = 0;

% Set positions of graphic objects
axisp   = [.23 .35 .75 .55];
fcolor  = [0.5 0.5 0.5];

lfieldp = [.25 .08 .10 .05];
lhip    = lfieldp + [.15 .06 0 0];
llop    = lhip + [0 -0.13 0 0];
lslidep = lfieldp + [.11 -0.07 -0.07 0.13];
ltextp  = lfieldp + [0 0.05 0 0];

sfieldp = [.60 .08 .10 .05];
ship    = sfieldp + [.15 .06 0 0];
slop    = ship + [0 -0.13 0 0];
sslidep = sfieldp + [.11 -0.07 -0.07 0.13];
stextp  = sfieldp + [0 0.05 0 0];

xfieldp = [.54 .24 .13 .05];
yfieldp = [0.01 .62 .13 .05];

% Set axis limits and data
xrange   = [p1low - 3 * p2high p1high + 3 * p2high];
yrange   = [0 1.1];
xvalues  = [p1low - 3 * p2high:0.1*parameter2:p1high + 3 * p2high];
yvalues  = cdf(distribution,xvalues,parameter1,parameter2);
p1_text  = 'Mu';
p2_text  = 'Sigma';

%   Create Cumulative Distribution Plot
dist_fig = figure;
set(dist_fig,'Units','Normalized','UserData','dist_fig','Backingstore','off');
figcolor = get(dist_fig,'Color');
if figcolor == [0 0 0]
   gray_axes = axes;
   set(gray_axes,'XTick',[],'YTick',[],'Units','Normalized','Position',[0 0 1 1]);
   set(gray_axes,'Color',fcolor);
   set(gray_axes,'ButtonDownFcn','disttool(''stray_click'')');
   axiscolor = figcolor;
else
   axiscolor = 1-figcolor;  
end

dist_axes = axes;
set(dist_fig,'CurrentAxes',dist_axes);
density_line = plot(xvalues,yvalues,'w-','LineWidth',2);
set(dist_axes,'Color',axiscolor,'NextPlot','add','DrawMode','fast');
set(dist_axes,'Position',axisp,'XLim',xrange,'YLim',yrange,'Box','on');

% Define graphics objects
p1_field=uicontrol('Style','edit','Units','normalized','Position',lfieldp,...
         'String',num2str(parameter1),...
         'BackgroundColor','white',...
         'CallBack','disttool(''setp1field'')');
         
p1_hi=uicontrol('Style','edit','Units','normalized','Position',lhip,...
         'String',num2str(p1high),...
         'BackgroundColor','white',...
         'CallBack','disttool(''setp1hi'')');
         
p1_lo=uicontrol('Style','edit','Units','normalized','Position',llop,...
         'String',num2str(p1low),...
         'BackgroundColor','white',... 
         'CallBack','disttool(''setp1lo'')');

p1_slider=uicontrol('Style','slider','Units','normalized','Position',lslidep,...
        'Value',parameter1,'Max',p1high,'Min',p1low,...
        'Callback','disttool(''setp1slider'')');

p2_field=uicontrol('Style','edit','Units','normalized','Position',sfieldp,...
         'String',num2str(parameter2),...
         'BackgroundColor','white',...
         'CallBack','disttool(''setp2field'')');
         
p2_hi=uicontrol('Style','edit','Units','normalized','Position',ship,...
         'String',num2str(p2high),...
         'BackgroundColor','white',...
         'CallBack','disttool(''setp2hi'')');

p2_lo=uicontrol('Style','edit','Units','normalized','Position',slop,...
         'String',num2str(p2low),...
         'BackgroundColor','white',...
         'CallBack','disttool(''setp2lo'')');

p2_slider=uicontrol('Style','slider','Units','normalized','Position',sslidep,...
        'Value',parameter2,'Max',p2high,'Min',p2low,...
        'Callback','disttool(''setp2slider'')');
                   
p3_field=uicontrol('Visible','off','Style','edit',...
         'String',num2str(parameter3),...
         'BackgroundColor','white',...
         'CallBack','disttool(''setp3field'')');
         
p3_hi=uicontrol('Visible','off','Style','edit',...
         'String',num2str(2),...
         'BackgroundColor','white',...
         'CallBack','disttool(''setp3hi'')');

p3_lo=uicontrol('Visible','off','Style','edit',...
         'String',num2str(0.1),...
         'BackgroundColor','white',...
         'CallBack','disttool(''setp3lo'')');

p3_slider=uicontrol('Visible','off','Style','slider',...
        'Value',parameter3,'Max',2,'Min',0.1,...
        'Callback','disttool(''setp3slider'')');
                   
p1_text=uicontrol('Style','text','Units','normalized','Position',ltextp,'UserData','p1_text',...
        'BackgroundColor',fcolor,...
        'ForegroundColor','white','String',p1_text);
        
p2_text=uicontrol('Style','text','Units','normalized','Position',stextp,'UserData','p2_text',...
        'BackgroundColor',fcolor,...
        'ForegroundColor','white','String',p2_text);

p3_text=uicontrol('Style','text','UserData','p3_text',...
        'Visible','off','BackgroundColor',fcolor,...
        'ForegroundColor','white');
      
yaxis_text=uicontrol('Style','text','Units','normalized','Position',yfieldp + [0 0.05 0 -0.01],...
        'BackgroundColor',fcolor,'UserData','yaxis_text',...
        'ForegroundColor','white','String','Probability');

popup=uicontrol('Style','Popup','String',...
'Beta|Binomial|Chi-square|Discrete Uniform|Exponential|F|Gamma|Geometric|Lognormal|Negative Binomial|Noncentral F|Noncentral T|Noncentral Chi-square|Normal|Poisson|Rayleigh|T|Uniform|Weibull',...
        'Units','normalized','Position',[.41 .92 .25 .06],'UserData','popup','Value',14,...
        'CallBack','disttool(''changedistribution'')');


functionpopup=uicontrol('Style','Popup','String',...
        'CDF|PDF','Value',1,'UserData','functionpopup',...
        'Units','normalized','Position',[.71 .92 .15 .06],...
        'CallBack','disttool(''changefunction'')');
        
x_field=uicontrol('Style','edit','Units','normalized','Position',xfieldp,...
         'String',num2str(newx),...
         'BackgroundColor','white','UserData','x_field',...
         'CallBack','disttool(''editX'')');
         
y_field=uicontrol('Style','edit','Units','normalized','Position',yfieldp,...
         'BackgroundColor','white','UserData','y_field',...
         'String',num2str(newy),'CallBack','disttool(''editY'')');

distcontrol = uicontrol('Style','text','Position',[0 0 .04 .04],...
        'String','distcontrol','UserData',distribution);

close_button = uicontrol('Style','Pushbutton','Units','normalized',...
               'Position',[0.01 0.01 0.13 0.05],'Callback','close','String','Close');

p1limits = uicontrol('Style','text','Position',[0 0.05 .04 .04],...
        'String','p1limits','UserData',p1lim);
      
p2limits = uicontrol('Style','text','Position',[0 0.1 .04 .04],...
        'String','p2limits','UserData',p2lim);


%   Create Reference Lines
yvertical = [yrange(1) yrange(2)]';
xvertical  = ones(size(yvertical)) * (xrange(1) + xrange(2))/2;
newx  = xvertical(1);
newy  = cdf(distribution,newx,parameter1,parameter2);
set(x_field,'String',num2str(newx));
set(y_field,'String',num2str(newy));


xhorizontal = [xrange(1) xrange(2)]';
yhorizontal = newy * ones(2,1);


vwitnessline = plot(xvertical,yvertical,'w-.','EraseMode','xor','UserData','vwitnessline');
hwitnessline = plot(xhorizontal,yhorizontal,'w-.','EraseMode','xor','UserData','hwitnessline');

set(vwitnessline,'ButtonDownFcn','disttool(''down'',1)');
set(hwitnessline,'ButtonDownFcn','disttool(''down'',2)');

set(dist_fig,'Backingstore','off','WindowButtonMotionFcn','disttool(''motion'',0)');
set(dist_fig,'WindowButtonDownFcn','disttool(''down'',1)');
set(gca,'Userdata',[density_line;vwitnessline;hwitnessline]);  
set(dist_fig,'Userdata',[p1_field;p1_hi;p1_lo;p1_slider;p2_field;p2_hi;p2_lo;...
    p2_slider;p1_text;p2_text;yaxis_text;popup;functionpopup;x_field;y_field;...
   distcontrol;p1limits;p2limits;dist_axes;p3_field;p3_hi;p3_lo;p3_slider;p3_text]);
% End of initialization activities.
elseif strcmp(action,'motion'),
   xrange = get(gca,'Xlim');
   yrange = get(gca,'Ylim');

    if flag == 0,
      cursorstate = get(gcf,'Pointer');
        cp = get(gca,'CurrentPoint');
        cx = cp(1,1);
        cy = cp(1,2);
        fuzzx = 0.01 * (xrange(2) - xrange(1));
        fuzzy = 0.01 * (yrange(2) - yrange(1));
        online = cy > yrange(1) & cy < yrange(2) & cx > xrange(1) & cx < xrange(2) &...
           ((cy > newy - fuzzy & cy < newy + fuzzy) | (cx > newx - fuzzx & cx < newx + fuzzx));
        if online & strcmp(cursorstate,'arrow'),
            set(gcf,'Pointer','crosshair');
        elseif ~online & strcmp(cursorstate,'crosshair'),
            set(gcf,'Pointer','arrow');
        end
    
    elseif flag == 1
        cp = get(gca,'CurrentPoint');
        newx=cp(1,1);
        if popupvalue == 2 | popupvalue == 4 | ...
         popupvalue == 8 | popupvalue == 10 | popupvalue == 15,
            newx = round(newx);
        end
        if newx > xrange(2)
            newx = xrange(2);
        end
        if newx < xrange(1)
            newx = xrange(1);
        end

        if functiontype == 1
           if popupvalue == 11 
              newy  = cdf(distribution,newx,parameter1,parameter2,parameter3);
           else  
              newy  = cdf(distribution,newx,parameter1,parameter2);
           end
        else
           if popupvalue == 11 
              newy  = pdf(distribution,newx,parameter1,parameter2,parameter3);
           else  
              newy  = pdf(distribution,newx,parameter1,parameter2);
           end
        end

        set(x_field,'String',num2str(newx));
        set(y_field,'String',num2str(newy));
        set(vwitnessline,'XData',newx*ones(2,1),'YData',yrange);
        set(hwitnessline,'XData',xrange,'YData',newy*ones(size(xrange)));

    elseif flag == 2
        cp = get(gca,'CurrentPoint');
        newy=cp(1,2);
        if newy > yrange(2)
            newy = yrange(2);
        end
        if newy < yrange(1)
            newy = yrange(1);
        end

     if popupvalue == 11
         newx  = icdf(distribution,newy,parameter1,parameter2,parameter3);
     else  
         newx  = icdf(distribution,newy,parameter1,parameter2);
     end

     set(x_field,'String',num2str(newx));
     set(y_field,'String',num2str(newy));
     set(vwitnessline,'XData',newx*ones(size(yrange)),'YData',yrange);
     set(hwitnessline,'XData',xrange,'YData',newy*ones(size(xrange)));

    end

elseif strcmp(action,'down'),
   xrange = get(gca,'Xlim');
   yrange = get(gca,'Ylim');
   
   set(gcf,'Pointer','crosshair');
   cp = get(gca,'CurrentPoint');
   newx=cp(1,1);
   if popupvalue == 2 | popupvalue == 4 | ...
      popupvalue == 8 | popupvalue == 10 | popupvalue == 15,
      newx = round(newx);
   end
   if newx > xrange(2)
      newx = xrange(2);
   end
   if newx < xrange(1)
      newx = xrange(1);
   end

   if functiontype == 1
      if popupvalue == 11
         newy  = cdf(distribution,newx,parameter1,parameter2,parameter3);
     else  
         newy  = cdf(distribution,newx,parameter1,parameter2);
      end
   else
      if popupvalue == 11
         newy  = pdf(distribution,newx,parameter1,parameter2,parameter3);
     else  
         newy  = pdf(distribution,newx,parameter1,parameter2);
      end
   end

   set(x_field,'String',num2str(newx));
   set(y_field,'String',num2str(newy));
   set(vwitnessline,'XData',newx*ones(2,1),'YData',yrange);
   set(hwitnessline,'XData',xrange,'YData',newy*ones(size(xrange)));
   
   if flag == 1
        set(gcf,'WindowButtonMotionFcn','disttool(''motion'',1)');
   elseif flag == 2
        set(gcf,'WindowButtonMotionFcn','disttool(''motion'',2)');
   end
   set(gcf,'WindowButtonUpFcn','disttool(''up'')');

elseif strcmp(action,'up'),
    set(gcf,'WindowButtonMotionFcn','disttool(''motion'',0)');
    set(gcf,'WindowButtonUpFcn','');
    
elseif strcmp(action,'editX'),
   xrange = get(gca,'XLIM');
   yrange = get(gca,'Ylim');
    newx=str2num(get(x_field,'String'));
    if newx > xrange(2)
        newx = xrange(2);
        set(x_field,'String',num2str(newx));
    end
    if newx < xrange(1)
        newx = xrange(1);
        set(x_field,'String',num2str(newx));
    end
    if functiontype == 1
      if popupvalue == 11
           newy  = cdf(distribution,newx,parameter1,parameter2,parameter3);
      else
           newy  = cdf(distribution,newx,parameter1,parameter2);
      end
    else
      if popupvalue == 11
           newy  = pdf(distribution,newx,parameter1,parameter2,parameter3);
      else
           newy  = pdf(distribution,newx,parameter1,parameter2);
      end
    end

    set(y_field,'String',num2str(newy));
    set(vwitnessline,'XData',newx*ones(size(yrange)));
    set(hwitnessline,'XData',xrange,'YData',newy*ones(size(xrange)));

elseif strcmp(action,'editY'),
   xrange = get(gca,'XLIM');
   yrange = get(gca,'Ylim');
    newy=str2num(get(y_field,'String'));
    if newy > 1
        newy = 1;
        set(y_field,'String',num2str(newy));
    end
    if newy < 0
        newy = 0;
        set(y_field,'String',num2str(newy));
    end
    newx  = icdf(distribution,newy,parameter1,parameter2);
    set(vwitnessline,'XData',newx*ones(size(yrange)));
    set(hwitnessline,'XData',xrange,'YData',newy*ones(size(xrange)));
    set(x_field,'String',num2str(newx));

elseif strcmp(action,'setdata'),
   xrange = get(dist_axes,'Xlim');
   yrange = get(dist_axes,'Ylim');
    set(gcf,'Pointer','watch');
      if popupvalue == 1  %Beta
        xrange  = [0 1];
        xvalues = [0.01:0.01:0.99];
    elseif popupvalue == 2 | popupvalue == 4 % Binomial or Discrete Uniform
        xrange  = [0 p1high];
        if functiontype == 2
            xvalues = [0:p1high];
        else
            xvalues = [0:p1high];
            xvalues = [xvalues - sqrt(eps);xvalues];
            xvalues = xvalues(:)';
        end
    elseif popupvalue == 3 % Chi-square
        xrange  = [0 p1high + 4 * sqrt(2 * p1high)];
        xvalues = linspace(0,xrange(2));
    elseif popupvalue == 5 % Exponential
        xrange  = [0 4*p1high];
        xvalues = [0:0.1*parameter1:4*p1high];
    elseif popupvalue == 6 % F
        hixvalue = finv(0.995,p1low,p2low);
        xrange  = [0 hixvalue];
        xvalues = linspace(xrange(1),xrange(2));
    elseif popupvalue == 7 % Gamma
        hixvalue = p1high * p2high + 4*sqrt(p1high * p2high * p2high);
        xrange  = [0 hixvalue];
        xvalues = linspace(0,hixvalue);
    elseif popupvalue == 8 % Geometric
        hixvalue = geoinv(0.99,p1low);
        xrange  = [0 hixvalue];
        if functiontype == 2
            xvalues = [0:round(hixvalue)];
        else
            xvalues = [0:round(hixvalue)];
            xvalues = [xvalues - sqrt(eps);xvalues];
            xvalues = xvalues(:)';
        end
    elseif popupvalue == 9 % Lognormal
       xrange = [0 logninv(0.99,p1high,p2high)];
        xvalues = linspace(0,xrange(2));
   elseif popupvalue == 10 % Negative Binomial
       xrange = [0 nbininv(0.99,p1high,p2low)];
      xvalues = (0:xrange(2));
      if functiontype == 1
            xvalues = [xvalues - sqrt(eps);xvalues];
            xvalues = xvalues(:)';
        end        
    elseif popupvalue == 11 % Noncentral F
        xrange = [0 p3high+30];
        xvalues = linspace(sqrt(eps),xrange(2));
    elseif popupvalue == 12 % Noncentral T 
        xrange = [p2high-14 p2high+14];
        xvalues = linspace(xrange(1),xrange(2));
    elseif popupvalue == 13 % Noncentral Chi-square
        xrange = [0 p2high+30];
        xvalues = linspace(sqrt(eps),xrange(2));
   elseif popupvalue == 14 % Normal distribution
        xrange   = [p1low - 3 * p2high p1high + 3 * p2high];
        xvalues  = [p1low - 3 * p2high:0.2*parameter2:p1high + 3 * p2high];
    elseif popupvalue == 15 % Poisson distribution
        xrange  = [0 4*p1high];
        if functiontype == 2
            xvalues = [0:round(4*parameter1)];
        else
            xvalues = [0:round(4*parameter1)];
            xvalues = [xvalues - sqrt(eps);xvalues];
            xvalues = xvalues(:)';
        end
    elseif popupvalue == 16 % Rayleigh
        hixvalue = raylinv(0.995,p1high);
        xrange  = [0 hixvalue];
        xvalues = linspace(xrange(1),xrange(2));
    elseif popupvalue == 17  % T distribution
        lowxvalue = tinv(0.005,p1low);
        xrange  = [lowxvalue -lowxvalue];
        xvalues = linspace(xrange(1),xrange(2));
    elseif popupvalue == 18 % Uniform distributioin
        xrange  = [p1low p2high];
        if functiontype == 1;
            xvalues = [p1low parameter1 + eps parameter2 - eps p2high];
        else
            xvalues = [parameter1  parameter2 ];
        end
        p1lim(2)   = p2low - eps;
        p2lim(1)   = p1high + eps;
    elseif popupvalue == 19 % Weibull
        hixvalue = weibinv(0.995,p1low,p2low);
        xrange  = [0 hixvalue];
        xvalues = linspace(xrange(1),xrange(2));
    end

    if newx >= xrange(2) | newx <= xrange(1) 
        newx    = (xrange(1) + xrange(2))/2;
        set(x_field,'String',num2str(newx));
    end

    if functiontype == 1
      if popupvalue == 11
           newy  = cdf(distribution,newx,parameter1,parameter2,parameter3);
           yvalues  = cdf(distribution,xvalues,parameter1,parameter2,parameter3);
      else
           newy  = cdf(distribution,newx,parameter1,parameter2);
           yvalues  = cdf(distribution,xvalues,parameter1,parameter2);
      end
        yrange = [0 1.1];
    else
      if popupvalue == 11
           newy  = pdf(distribution,newx,parameter1,parameter2,parameter3);
           yvalues  = pdf(distribution,xvalues,parameter1,parameter2,parameter3);
      else
           newy  = pdf(distribution,newx,parameter1,parameter2);
           yvalues  = pdf(distribution,xvalues,parameter1,parameter2);
      end
 
        if flag == 1
            if popupvalue == 1  % Beta density.
                tempx = [0.01 0.1:0.1:0.9 0.99];
                temp1 = linspace(p1low,p1high,21);
                temp2 = linspace(p2low,p2high,21);
                [x p1 p2] = meshgrid(tempx,temp1,temp2);
                y = pdf(distribution,x,p1,p2);
            elseif popupvalue == 2 % Binomial density.
                y = 1;
            elseif popupvalue == 3 % Chi-square density.
                tempx = linspace(xrange(1),xrange(2),101);
                y = pdf(distribution,tempx,p1low);
            elseif popupvalue == 4 % Discrete Uniform density.
                y = 1 ./ p1low;
            elseif popupvalue == 5 % Exponential density.
                y = 1 / p1low;
            elseif popupvalue == 6 % F density.
                tempx = linspace(xrange(1),xrange(2),101);
                temp1 = [p1low:p1high];
                temp2 = [p2low:p2high];
                [x p1 p2] = meshgrid(tempx,temp1,temp2);                
                y = pdf(distribution,x,p1,p2);
           elseif popupvalue == 7 % Gamma density.
                tempx = [0.1 linspace(xrange(1),xrange(2),101)];
                temp1 = linspace(p1low,p1high,11);
                temp2 = linspace(p2low,p2high,11);
                [x p1 p2] = meshgrid(tempx,temp1,temp2);
                y = pdf(distribution,x,p1,p2);
           elseif popupvalue == 8 % Geometric density.
                y = p1high;
           elseif popupvalue == 9 % Lognormal density.
              x = exp(linspace(p1low,p1low+0.5*p2low.^2));
                y = pdf(distribution,x,p1low,p2low);
           elseif popupvalue == 10 % Negative Binomial density.
                y = 0.91;
           elseif popupvalue == 11 % Noncentral F density.
                y = 0.4;
           elseif popupvalue == 12 % Noncentral T density.
                y = 0.4;
           elseif popupvalue == 13 % Noncentral Chi-square density.
                y = 0.4;
           elseif popupvalue == 14 % Normal density.
                y = pdf(distribution,0,0,p2low);
           elseif popupvalue == 15 % Poisson density.
                y = pdf(distribution,[0 p1low],p1low);
           elseif popupvalue == 16 % Rayleigh density.
                y = 0.6;
           elseif popupvalue == 17 % T density.
                y = pdf(distribution,0,p1high);
           elseif popupvalue == 18 % Uniform density.
                y = 1 ./ (p2low - p1high);
           elseif popupvalue == 19 % Weibull density.
                tempx = [0.05 linspace(xrange(1),xrange(2),21)];
                temp1 = linspace(p1low,p1high,21);
                temp2 = linspace(p2low,p2high,21);
                [x p1 p2] = meshgrid(tempx,temp1,temp2);
                y = pdf(distribution,x,p1,p2);
           end
            k1 = find(isnan(y) | isinf(y));
            if any(k1),
                y(k1) = [];
            end
            yrange = [0 abs(1.1 .* max(max(y)))];
        end
    end

    set(y_field,'String',num2str(newy));
    set(vwitnessline,'XData',newx*ones(size(yrange)),'YData',yrange);
    set(hwitnessline,'XData',xrange,'YData',newy*ones(size(xrange)));
    set(gcf,'pointer','arrow');
    set(gca,'XLim',xrange, 'YLim',yrange);
    set(density_line,'XData',xvalues,'Ydata',yvalues,'Color','w');
    
elseif strcmp(action,'setp1slider'),
    cv = get(p1_slider,'Value');
    if popupvalue ~= 2 & popupvalue ~= 3 & popupvalue ~= 4 ...
        & popupvalue ~= 6 & popupvalue ~= 10 & popupvalue ~= 12 ...
        & popupvalue ~= 13 & popupvalue ~= 14 & popupvalue ~= 17
        parameter1 = cv;
    else
        parameter1 = round(cv);
    end
    set(p1_field,'String',num2str(parameter1));
    disttool('setdata',0);  
    
elseif strcmp(action,'setp1field'),
    cv = str2num(get(p1_field,'String'));
    if cv > p1lim(1) & cv < p1lim(2),
        if popupvalue ~= 2 & popupvalue ~= 3 & popupvalue ~= 4 ...
           & popupvalue ~= 6 & popupvalue ~= 10 & popupvalue ~= 12 ...
           & popupvalue ~= 13 & popupvalue ~= 14 & popupvalue ~= 17
            parameter1 = cv;
        else
            parameter1 = round(cv);
            set(p1_field,'String',num2str(parameter1));
        end
        if (parameter1 > p1high), 
            set(p1_hi,'String',num2str(parameter1));
            p1high = parameter1;
            set(p1_slider,'Max',p1high);
            disttool('setdata',1);
        end;
        if (parameter1 < p1low), 
            set(p1_lo,'String',num2str(parameter1));
            p1low=parameter1;
            set(p1_slider,'Min',p1low);
            disttool('setdata',1);  
        end;
        set(p1_slider,'Value',parameter1);
        disttool('setdata',0);
    else
        set(p1_field,'String',num2str(parameter1));
    end

elseif strcmp(action,'setp1hi'),
    cv = str2num(get(p1_hi,'String'));
    if cv > p1lim(1) & cv < p1lim(2),
        if cv > parameter1,
           if popupvalue ~= 2 & popupvalue ~= 3 & popupvalue ~= 4 ...
              & popupvalue ~= 6 & popupvalue ~= 10 & popupvalue ~= 12 ...
              & popupvalue ~= 13 & popupvalue ~= 14 & popupvalue ~= 17
                p1high = cv;
            else
                p1high = round(cv);
                set(p1_hi,'String',num2str(p1high));
            end
            set(p1_slider,'Max',p1high);
            disttool('setdata',1);  
        else
           if popupvalue ~= 2 & popupvalue ~= 3 & popupvalue ~= 4 ...
              & popupvalue ~= 6 & popupvalue ~= 10 & popupvalue ~= 12 ...
              & popupvalue ~= 13 & popupvalue ~= 14 & popupvalue ~= 17
                set(p1_hi,'String',num2str(p1high));
            else
                set(p1_hi,'String',num2str(round(p1high)));
            end
        end
    else
        set(p1_hi,'String',num2str(p1high));
    end
           
elseif strcmp(action,'setp1lo'),
    cv = str2num(get(p1_lo,'String'));
    if cv > p1lim(1) & cv < p1lim(2),
        if cv < parameter1,
           if popupvalue ~= 2 & popupvalue ~= 3 & popupvalue ~= 4 ...
              & popupvalue ~= 6 & popupvalue ~= 10 & popupvalue ~= 12 ...
              & popupvalue ~= 13 & popupvalue ~= 14 & popupvalue ~= 17
                p1low = cv;
            else
                p1low = round(cv);
                set(p1_lo,'String',num2str(p1low))
            end
            set(p1_slider,'Min',p1low);
            disttool('setdata',1);
        else 
           if popupvalue ~= 2 & popupvalue ~= 3 & popupvalue ~= 4 ...
              & popupvalue ~= 6 & popupvalue ~= 10 & popupvalue ~= 12 ...
              & popupvalue ~= 13 & popupvalue ~= 14 & popupvalue ~= 17
                set(p1_lo,'String',num2str(p1low));
            else
                set(p1_lo,'String',num2str(round(p1low)));
            end
            set(p1_lo,'String',num2str(p1low));
        end
    else
        set(p1_lo,'String',num2str(p1low));
    end

elseif strcmp(action,'setp2slider'),
    cv = get(p2_slider,'Value');
    if popupvalue ~= 6 & popupvalue ~= 12
        parameter2 = cv;
    else
        parameter2 = round(cv);
    end
    set(p2_field,'String',num2str(parameter2));
    disttool('setdata',0);

elseif strcmp(action,'setp2field'),
    cv = str2num(get(p2_field,'String'));
    if cv > p2lim(1) & cv < p2lim(2),
        if popupvalue ~= 6 & popupvalue ~= 12
            parameter2 = cv;
        else
            parameter2 = round(cv);
            set(p2_field,'String',num2str(parameter2));
        end
        if (parameter2 > p2high),
            p2high = parameter2;
            set(p2_hi,'String',num2str(parameter2));
            set(p2_slider,'Max',p2high);    
            disttool('setdata',1);  
        end;
        if (parameter2 < p2low),
            p2low = parameter2; 
            set(p2_lo,'String',num2str(parameter2),'Min',p2low);
            set(p2_slider,'Min',p2low);
            disttool('setdata',1);  
        end
        set(p2_slider,'Value',parameter2);
        disttool('setdata',0);
    else 
        set(p2_field,'String',num2str(parameter2));
    end

elseif strcmp(action,'setp2hi'),
    cv = str2num(get(p2_hi,'String'));
    if cv > p2lim(1) & cv < p2lim(2),
        if cv > parameter2, 
            if popupvalue ~= 6 & popupvalue ~= 12
                p2high = cv;
            else
                p2high = round(cv);
                set(p2_hi,'String',num2str(p2high));
            end
            set(p2_slider,'Max',p2high);    
            disttool('setdata',1);  
        else 
            set(p2_hi,'String',num2str(p2high));
        end
    else
        set(p2_hi,'String',num2str(p2high));
    end 

elseif strcmp(action,'setp2lo'),
    cv = str2num(get(p2_lo,'String'));
    if cv > p2lim(1) & cv < p2lim(2),
        if cv < parameter2 
            if popupvalue ~= 6 & popupvalue ~= 12
                p2low = cv;
            else
                p2low = round(cv);
                set(p2_lo,'String',num2str(p2low));
            end
            set(p2_slider,'Min',p2low);
            disttool('setdata',1);  
        else 
            set(p2_lo,'String',num2str(p2low));
        end
    else
        set(p2_lo,'String',num2str(p2low)); 
    end
% Change 3rd parameter (for Noncentral F only)
elseif strcmp(action,'setp3slider'),
    cv = get(p3_slider,'Value');
    set(p3_field,'String',num2str(cv));
    disttool('setdata',0);

elseif strcmp(action,'setp3field'),
    cv = str2num(get(p3_field,'String'));
    if cv > 0,
            parameter3 = cv;
        if (parameter3 > p3high),
            p3high = parameter3;
            set(p3_hi,'String',num2str(parameter3));
            set(p3_slider,'Max',p3high);    
            disttool('setdata',1);  
        end;
        if (parameter3 < p3low),
            p3low = parameter3; 
            set(p3_lo,'String',num2str(parameter3),'Min',p3low);
            set(p3_slider,'Min',p3low);
            disttool('setdata',1);  
        end
        set(p3_slider,'Value',parameter3);
        disttool('setdata',0);
    else 
        set(p2_field,'String',num2str(parameter2));
    end

elseif strcmp(action,'setp2hi'),
    cv = str2num(get(p3_hi,'String'));
    if cv > 0,
        if cv > parameter3, 
            p3high = cv;
            set(p3_slider,'Max',p3high);    
            disttool('setdata',1);  
        else 
            set(p3_hi,'String',num2str(p3high));
        end
    else
        set(p3_hi,'String',num2str(p3high));
    end 

elseif strcmp(action,'setp3lo'),
    cv = str2num(get(p3_lo,'String'));
    if cv > 0,
        if cv < parameter3 
            p3low = cv;
            set(p3_slider,'Min',p3low);
            disttool('setdata',1);  
        else 
            set(p3_lo,'String',num2str(p3low));
        end
    else
        set(p3_lo,'String',num2str(p3low)); 
    end


elseif strcmp(action,'changedistribution'),
   parameter1 = [2 10 5 20 1 5 2 0.5 0.5 2 5 5 5 0 5 2 5 0 2];
   p1high     = [4 10 10 20 2 10 5 1 1 3 10 10 10 2 5 5 10 0.5 3];
   p1low      = [0.5 2 1 5 0.5 5 2 0.25 0 1 2 2 2 -2 1 1 2 0 1];
   p1lim      = [0 0 0 0 0 0 0 0 -Inf 1 0 0 0 -Inf 0 0 0 -Inf 0;...
              Inf Inf Inf Inf Inf Inf Inf 1 Inf Inf Inf Inf Inf Inf Inf Inf Inf (p2low - eps) Inf];

   parameter2 = [2 0.5 NaN NaN NaN 5 2 NaN 0.25 0.6 5 1 1 1 NaN NaN NaN 1 2];
   p2high     = [4 1 NaN NaN NaN 10 5 NaN 0.3 0.99 10 2 2 2 NaN NaN NaN 2 3];
   p2low      = [0.5 0 NaN NaN NaN 5 2 NaN 0.2 0.5 2 0.01 0.01 0.5 NaN NaN NaN 1 1];
   p2lim      = [0 (0 - eps) NaN NaN NaN 0 0 NaN 0 0 0 0 0 0 NaN 0 NaN 0 0;...
              Inf (1+eps) NaN NaN NaN Inf Inf NaN Inf 1 Inf Inf Inf Inf NaN Inf NaN Inf Inf];

    if popupvalue == 1         %Beta distribution
        set(p1_text,'String','A');
        set(p2_text,'String','B');
        set(distcontrol,'Userdata','Beta');
      disttool('makevisible');

    elseif popupvalue == 2      %Binomial distribution
        set(p1_text,'String','Trials');
        set(p2_text,'String','Prob');
       set(distcontrol,'Userdata','Binomial');
        disttool('makevisible');

    elseif popupvalue == 3      %Chisquare distribution
       set(distcontrol,'Userdata','Chisquare');
        set(p1_text,'String','df');

    elseif popupvalue == 4      %Discrete Uniform Distribution
       set(distcontrol,'Userdata','Discrete Uniform');
        set(p1_text,'String','Number');
       
    elseif popupvalue == 5      %Exponential distribution
       set(distcontrol,'Userdata','Exponential');
       set(p1_text,'String','Lambda');
       
    elseif popupvalue == 6      %F distribution
       set(distcontrol,'Userdata','F');
        set(p1_text,'String','df1');
        set(p2_text,'String','df2');
        disttool('makevisible');        

    elseif popupvalue == 7      %Gamma distribution
       set(distcontrol,'Userdata','Gamma');
       set(p1_text,'String','A');
       set(p2_text,'String','B');
       disttool('makevisible');

    elseif popupvalue == 8      %Geometric distribution
       set(distcontrol,'Userdata','Geometric');
       set(p1_text,'String','Prob');

    elseif popupvalue == 9      %Lognormal distribution
       set(distcontrol,'Userdata','Lognormal');
       set(p1_text,'String','Mu');
       set(p2_text,'String','Sigma');
       disttool('makevisible');

    elseif popupvalue == 10      %Negative Binomial distribution
       set(distcontrol,'Userdata','Negative Binomial');
       set(p1_text,'String','R');
       set(p2_text,'String','Prob');
       disttool('makevisible');

    elseif popupvalue == 11      %Noncentral F distribution
       set(distcontrol,'Userdata','Noncentral F');
        set(p1_text,'String','df1');
        set(p2_text,'String','df2');
        set(p3_text,'String','Lambda');
        disttool('makevisible');        

    elseif popupvalue == 12      %Noncentral T distribution
       set(distcontrol,'Userdata','Noncentral T');
       set(p1_text,'String','df');
       set(p2_text,'String','delta');
       disttool('makevisible');        

    elseif popupvalue == 13      %Noncentral Chi-square distribution
       set(distcontrol,'Userdata','Noncentral Chi-square');
        set(p1_text,'String','df');
        set(p2_text,'String','delta');
        disttool('makevisible');        
      
    elseif popupvalue == 14      %Normal distribution
       set(distcontrol,'Userdata','Normal');
       set(p1_text,'String','Mu');
       set(p2_text,'String','Sigma');
       disttool('makevisible');

    elseif popupvalue == 15      %Poisson distribution
       set(distcontrol,'Userdata','Poisson');
       set(p1_text,'String','Lambda');

    elseif popupvalue == 16      %Rayleigh distribution
       set(distcontrol,'Userdata','Rayleigh');
       set(p1_text,'String','B');
                 
    elseif popupvalue == 17      %T distribution
       set(distcontrol,'Userdata','T');
       set(p1_text,'String','df');
        
    elseif popupvalue == 18      %Uniform distribution
       set(distcontrol,'Userdata','Uniform');
       set(p1_text,'String','Min');
       set(p2_text,'String','Max');
       disttool('makevisible');
 
    elseif popupvalue == 19      %Weibull distribution   
       set(distcontrol,'Userdata','Weibull');
       set(p1_text,'String','A');
       set(p2_text,'String','B');
       disttool('makevisible');        
    end

   if popupvalue == 3 | popupvalue == 4 | popupvalue == 5 | popupvalue == 8 ...
      | popupvalue == 15 | popupvalue == 16  | popupvalue == 17  
       set(p2_text,'Visible','off');
        set(p2_slider,'Visible','off');
        set(p2_field,'Visible','off');
        set(p2_lo,'Visible','off');
        set(p2_hi,'Visible','off');
   end
 
    if popupvalue ~= 11 
       set(p3_text,'Visible','off');
        set(p3_slider,'Visible','off');
        set(p3_field,'Visible','off');
        set(p3_lo,'Visible','off');
        set(p3_hi,'Visible','off');
       set(p1_field,'Position',[.25 .08 .10 .05]);
        set(p1_hi,'Position',[.25 .08 .10 .05] + [.15 .06 0 0]);
        set(p1_lo,'Position',[.25 .08 .10 .05] + [.15 .06 0 0] + [0 -0.13 0 0]);
        set(p1_slider,'Position',[.25 .08 .10 .05] + [.11 -0.07 -0.07 0.13]);
        set(p1_text,'Position',[.25 .08 .10 .05] + [0 0.05 0 0]);

       set(p2_field,'Position',[.60 .08 .10 .05]);
        set(p2_hi,'Position',[.60 .08 .10 .05] + [.15 .06 0 0]);
        set(p2_lo,'Position',[.60 .08 .10 .05] + [.15 .06 0 0] + [0 -0.13 0 0]);
        set(p2_slider,'Position',[.60 .08 .10 .05] + [.11 -0.07 -0.07 0.13]);
        set(p2_text,'Position',[.60 .08 .10 .05] + [0 0.05 0 0]);

    else
       set(p1_field,'Position',[.25 .08 .08 .05]);
        set(p1_hi,'Position',[.25 .08 .08 .05] + [.13 .06 0 0]);
        set(p1_lo,'Position',[.25 .08 .08 .05] + [.13 -.07 0 0]);
        set(p1_slider,'Position',[.25 .08 .08 .05] + [.09 -0.07 -0.05 0.13]);
        set(p1_text,'Position',[.25 .08 .08 .05] + [0 0.05 0 0]);

       set(p2_field,'Position',[.50 .08 .08 .05]);
        set(p2_hi,'Position',[.50 .08 .08 .05] + [.13 .06 0 0]);
        set(p2_lo,'Position',[.50 .08 .08 .05] + [.13 -.07 0 0] );
        set(p2_slider,'Position',[.50 .08 .08 .05] + [.09 -0.07 -0.05 0.13]);
        set(p2_text,'Position',[.50 .08 .08 .05] + [0 0.05 0 0]);

       set(p3_field,'Position',[.75 .08 .08 .05]);
        set(p3_hi,'Position',[.75 .08 .08 .05] + [.13 .06 0 0]);
        set(p3_lo,'Position',[.75 .08 .08 .05] + [.13 -.07 0 0]);
        set(p3_slider,'Position',[.75 .08 .08 .05] + [.09 -0.07 -0.05 0.13]);
        set(p3_text,'Position',[.75 .08 .08 .05] + [0 0.05 0 0]);      

      set(p3_text,'Visible','on');
        set(p3_slider,'Visible','on');
        set(p3_field,'Visible','on');
        set(p3_lo,'Visible','on');
        set(p3_hi,'Visible','on');
   end
   
    set(density_line,'LineStyle','-')
    
    if functiontype == 1
        set(hwitnessline,'Visible','on');
    else
        set(hwitnessline,'Visible','off');
        if popupvalue == 2 | popupvalue == 4 | popupvalue == 8 | popupvalue == 10 | popupvalue == 15,
            set(density_line,'LineStyle','+');
        end
    end
    
    set(p1_field,'String',num2str(parameter1(popupvalue)));
    set(p2_field,'String',num2str(parameter2(popupvalue)));
    set(p1_lo,'String',num2str(p1low(popupvalue)));
    set(p2_lo,'String',num2str(p2low(popupvalue)));
    set(p1_hi,'String',num2str(p1high(popupvalue)));
    set(p2_hi,'String',num2str(p2high(popupvalue)));
    set(p1_slider,'Min',p1low(popupvalue),'Max',p1high(popupvalue),'Value',parameter1(popupvalue));
    set(p2_slider,'Min',p2low(popupvalue),'Max',p2high(popupvalue),'Value',parameter2(popupvalue));

    disttool('setdata',1);

% End of changedistribution action.

elseif strcmp(action,'changefunction'),
    disttool('setdata',1);
    if functiontype == 1;
        set(yaxis_text,'String','Probability');
        set(y_field,'Style','edit');
        set(hwitnessline,'Visible','on');
        yrange = [0 1.1];
        set(density_line,'LineStyle','-');
        set(y_field,'BackgroundColor','white');
    else
        set(yaxis_text,'String','Density');
        set(y_field,'Style','text');
        set(hwitnessline,'Visible','off');
        if popupvalue == 2 | popupvalue == 4 | popupvalue == 8 | popupvalue == 10 | popupvalue == 15,
            set(density_line,'LineStyle','+');
        end
        set(y_field,'BackgroundColor',[0.75 0.75 0.75]);
    end
    
elseif strcmp(action,'makevisible'),
        set(p2_text,'Visible','on');
        set(p2_slider,'Visible','on');
        set(p2_field,'Visible','on');
        set(p2_lo,'Visible','on');
        set(p2_hi,'Visible','on');
      
elseif strcmp(action,'stray_click'),
   h = get(density_line,'parent');
   set(gcf,'CurrentAxes',h);   
    
end     % End of long "case" statement.
