function clsdemo(demoname)
% Closes demos that are not used anymore.
% A cleaning function

%   Yama Habibzai
%   Copyright (c) 1984-98 by The MathWorks, Inc. All Rights Reserved
%   $Revision: 1.4 $  $Date: 1998/08/19 19:41:04 $

figs = allchild(0);
figNumber = findobj(figs, 'Name', 'MATLAB Tour');

param=get(figNumber, 'Userdata');

%Close any Simulink models 
simExist = exist('siminfo.m');
if ~isempty(simExist)
  SimModel = find_system('type', 'block_diagram');
  if ~isempty(SimModel)
    SimModel = char(SimModel);
    modelsize = size(SimModel);
    for i=1:modelsize(1)
      model = SimModel(i,:);
      model = deblank(model);
      % Stop the simulation first
      stat = get_param(model, 'simulationstatus');
      if strcmp(stat, 'running') 
	set_param(model, 'SimulationCommand', 'Stop');
      end
      close_system(model, 0);
    end  % for ...
  end   % if isempty ...
end   % Closing non-running models ...

% Store the previous demo name in figure userdata
% and only open another demo once it has been closed

prevdemo = param.prevdemo;
DemoFig  = param.DemoWin;

% If new selection is the same demo, check if its been closed
% if it's closed, then open it.

figs = allchild(0);
for i = 1:size(figs)
  figname = get(figs(i), 'Name');
  if strcmp(figname, 'MATLAB Help Window');
    close(figs(i))
    break
  end 
  if ~strcmp(figname, 'MATLAB Tour') & ~strcmp(figname, 'MATLAB Help Window')
    if ~isempty(DemoFig)
      if  strcmp(demoname, prevdemo)
	% do nothing 
      elseif strcmp(DemoFig, figname)
         if strcmp(figname, 'Where is MATLAB used?')
          % Don't close this window, just set the pointer to arrow
          figs = allchild(0);
          industryFig = findobj(figs, 'Name', 'Where is MATLAB used?');
          if ~isempty(industryFig)
            set(industryFig, 'pointer', 'arrow');
          end
         else
	   close(figs(i));
	   break;
         end % if...
	%elseif strcmp(demoname, prevdemo)
	% do nothing
      end
    end
  end
end


