function  [xx] = sviplot1(input);
% SVIPLOT1
%     [XX] = SVIPLOT1(INPUT)
%     This function is only used by SVITERBI.C
%
%     See also SVIPLOT2, SVIPLOT3.


%  Copyright 1996-2000 The MathWorks, Inc.
%       $Revision: 1.19 $

x(1) = input(1);
n_std_sta = input(2);
num_state = input(3);
plot_flag = input(4);
initial_flag = input(5);

if x(1) <= 0   
  % initialize the figure.
  sl_name = get_param(0,'CurrentSystem');
  block   = get_param(sl_name, 'CurrentBlock');
  sl_name_gcs = sl_name;
  
  [n_b, m_b]= size(block);
  if n_b > 1
    error('Something is wrong with get_param, You don''t have the current Simulink');
  elseif n_b < 1
    error('Cannot delete block during the simulation.');    
  end;

  % findout if the graphics window exist
  ind = find(sl_name == setstr(10));
  dash = '_';
  sl_name(ind)=dash(ones(size(ind)));

  % in the case of the figure exists
  Figures = allchild(0);
  new_figure = 1;
  i = 1;
  while ((new_figure) & (i <= length(Figures)))
    if strcmp(get(Figures(i), 'Type'), 'figure')
      if strcmp(get(Figures(i), 'Name'), sl_name)
	h_fig = Figures(i);
	handles = get(h_fig,'UserData');
	new_figure = 0;
	h_axis = handles(1);
	set(0,'CurrentF',h_fig);
	set(h_axis,'Xlim', [0, plot_flag-1], 'Ylim', [0, n_std_sta-1]);
	delete(allchild(h_axis));
	handles = handles(1);
      end;
    end;
    i = i + 1;
  end;
  if new_figure
    h_fig = figure;
    set(h_fig, 'Name', sl_name);
    handles = axes('Xlim',[0, plot_flag-1], ...
	'Ylim',[0, n_std_sta-1], ...
	'Parent', h_fig);
  end;
  handles = allchild(h_fig);
  set(handles, 'Visible', 'Off');
  set(h_fig,'HandleVisibility','off', 'Clipping', 'off');
  set(handles(1), 'NextPlot', 'add',...
      'HandleVisibility','Off');
  handles(2) = plot(0,0,'y-',...
      'Erasemode','normal',...
      'Parent', handles(1));
  handles(3) = plot(0,0,'r-',...
      'Erasemode','normal',...
      'Parent', handles(1),...
      'LineWidth',2);
  handles(4) = plot(-10,-10, 'go', ...
      'Parent', handles(1),...
      'Erasemode','normal');
  for i = 1 : n_std_sta
    handles(i+4) = text(0,0,'0',...
	'Parent', handles(1));
    set(handles(i+4), 'Color',[0,1 1]);
  end;
  for i = 0 : n_std_sta - 1
    handles(5+n_std_sta+i) = text(-plot_flag/8, i,...
	['State ', num2str(bi2de(fliplr(de2bi(i, num_state))))],...
	'Parent', handles(1));
  end;
  handles(5+2*n_std_sta) = text(plot_flag/2.1, -n_std_sta/15, 'Time',...
      'Parent', handles(1));
  set(h_fig, 'UserData',handles);
  sys(1) = h_fig;
  x(1) = h_fig;
  set_param(sl_name_gcs, 'userdata', x(1));
  closegr(sl_name_gcs);
end; 					% if x(1) < 0
  
plot_flag_test = allchild(0);

if isempty(plot_flag_test)
  plot_flag_test = 0;
elseif isempty(find(plot_flag_test==x(1)))
  plot_flag_test = 0;
else
  plot_flag_test = 1;
  handles = get(x(1), 'UserData');
  if length(handles) ~= (5+2*n_std_sta)
    plot_flag_test = 0;
  end;
end;

xx(1) = x(1);
xx(2) = plot_flag_test;
xx(3) = 12345;

%%%%%%%%%%%%%%%%%%%%%%
%    end sviplot1    %
%%%%%%%%%%%%%%%%%%%%%%

%
% CLOSEGR
%
function closegr(sl_name)
%CLOSEGR Close figures generated from block S-function.
%   CLOSEGR(BLOCK) builds a link between a Simulink block
%   and the graph is generates. If the block name changed, the
%   figure name is changed also. If the block diagram is closed, 
%   the figure is also closed. Read the functions SFUNY, SFUNXY, 
%   SFUNYST for the examples of how to use this function. 

% In the following Syntax
%  OpenFcn        is for double click
%  DeleteFcn      is for delete
%  CopyFcn        is for copy (update copy's user data)
%  LoadFcn        is for restoring open state of dialog
%  NameChangeFcn  is for name changes
%  ParentCloseFcn is for parental closure

del_fcn_str = 'y_tp=get_param(gcb,''userdata'');';
del_fcn_str=[del_fcn_str,'if ishandle(y_tp),delete(y_tp);end'];

set_param(sl_name,...
  'OpenFcn',        '',...
  'DeleteFcn',      del_fcn_str,...
  'CopyFcn',        'set_param(gcb,''userdata'',[]);',...
  'LoadFcn',        '',...
  'NameChangeFcn',  'set(get_param(gcb,''userdata''),''Name'',gcb);',...
  'ParentCloseFcn', '');

%%%%%%%%%%%%%%%
% end CLOSEGR %
%%%%%%%%%%%%%%%

