function  [sys, x0, str, ts]  = sfunxy(t,x,u,flag,ax,st)
%SFUNXY S-function that acts as an X-Y scope using MATLAB plotting functions.
%
%	This M-file is designed to be used in a SIMULINK S-function block.
%	It draws a line from the previous input point, which is stored using
%	discrete states, and the current point.  It then stores the current
%	point for use in the next invocation.
%
%	Set this M-file up in an S-function block with a two-input MUX 
%	feeding into it. Set the function parameter up as a four element
%	vector defining the axis of the graph.
%
%	See also SFUNXYS, LORENZS.

%	Copyright (c) 1990-94 by The MathWorks, Inc.
%	Andrew Grace 5-30-91.
%	Revised Wes Wang 4-28-93, 8-17-93, 12-15-93

if (abs(flag) == 2) | (flag == 9)   	% A real time hit 
  h_fig = x(2);
  % initialize the figure etc
  if (h_fig <= 0)
	if nargin < 5  
      disp('Axis not defined; using default axis');
      ax = [0 1 0 1];
	elseif length(ax)~=4
	  disp('Axis not defined correctly; it must be a 1x4 vector')
	  ax = [0 1 0 1];
	end
    % Initialize graph
	[sl_name,blocks] = get_param;
	[n_b, m_b]= size(blocks);
	if n_b < 1
      error('Cannot delete block while simulating')
	elseif n_b > 1
      error('Something wrong in get_param, You don''t have the current SIMULINK')
	end;
	% findout if the graphics window exist
	ind = find(sl_name == setstr(10));
	for i = ind
	  sl_name(i) = '_';
	end;

	% findout if the graphics window exist
	Figures = get(0,'Chil');
	new_figure = 1;
	for i = 1: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');
	      if length(handles) == 6
            new_figure = 0;
            h_axis = handles(1);
            h_plot = handles(2);
            set(h_plot,'XData',ax(1),'YData',ax(3),'Erasemode','none','UserData',u(2));
            set(h_axis,'Xlim', ax(1:2), 'Ylim', ax(3:4),'UserData',u(1)); 
          end;
        end;
      end;
    end;

    % the figure does not exist, and this is an end of simulation (flag 9) call
    % exit early
    if new_figure & (flag == 9)
      sys = [];
      return;
    end;
	 
    if new_figure
      h_fig = figure('Unit','pixel','Pos',[100 100 400 300], ...
           'Name',sl_name);
      set(0, 'CurrentF', h_fig);
      h_axis = axes('Xlim', ax(1:2), 'Ylim', ax(3:4),'UserData',u(1)); 
      h_plot =  plot([ax(1),ax(1)],[ax(3) ax(3)],'Erasemode','none');
      set(h_axis,'NextPlot','add');
      h_plot =  plot([ax(2),ax(2)],[ax(4) ax(4)],'r-','Erasemode','none','UserData',u(2));
      set(h_axis,'NextPlot','new');
      xl = get(h_axis,'XLabel');
      set(xl,'String','X Axis');
      xl = get(h_axis,'YLabel');
      set(xl,'String','Y Axis');
      xl = get(h_axis,'Title');
      set(xl,'String','X Y Plot');
    end;
    set(h_axis,'XGrid','on','YGrid','on');
    set(h_fig,'Color',get(h_fig,'Color'));
    set(h_fig,'NextPlot','new');
    x = [0; h_fig]; 	% Flag to indicate first point
    set(h_fig, 'UserData', [h_axis h_plot ax]);
  end;

  sys = x;
  handles = get(x(2),'UserData');
  if x(1) == 0
    x_data = [];
    y_data = [];
    x(1) = 0;
  else
    % Use none as Erasemode for fast plotting
    if length(ax) >= 4
      if (min(ax(1:4)==handles(3:6)) <= 0)
	handles(3:6) = [ax(1) ax(2) ax(3) ax(4)];
	set(handles(1),'Xlim',ax(1:2),'Ylim',ax(3:4));
	set(x(2), 'UserData', handles);
      end;
    end;
    x_data = get(handles(1),'UserData');
    y_data = get(handles(2),'UserData');
    set(handles(2),'XData',[x_data(x(1)),u(1)],'YData',[y_data(x(1)),u(2)]);
  end
  set(handles(1), 'UserData', [x_data, u(1)]);
  set(handles(2), 'UserData', [y_data, u(2)]);
  sys(1) = x(1) + 1;
  if flag == 9
    tmp_x = get(handles(1),'Xlim');
    tmp_y = get(handles(1),'Ylim');
    set(handles(2),'XData',x_data,'YData',y_data);
	set(handles(1),'Xlim',tmp_x/2);
	set(handles(1),'Xlim',tmp_x,'Ylim',tmp_y);
	sys=[];
  end;
	
elseif (flag == 4) & (nargin > 5)  
  if (st > 0)   
    sys = t + st;
  else
    sys = [];
  end
elseif flag  == 0, 	% Initialization
  
  % Return system sizes
  sys(1) = 0;		% 0 continuous states
  sys(2) = 2;	        % 2 discrete states
  sys(3) = 0;		% 0 outputs
  sys(4) = 2;		% 2 inputs
  sys(5) = 0;		% 0 roots
  sys(6) = 0;		% no direct feedthrough
  sys(7) = 1;		% 1 sample time
  
  x0 = [0; 0]; 	% Flag to indicate first point

  if (nargin > 5)
    ts = [st, 0];
  else
    ts = [-1, 0];
  end

else 

  sys = [];

end


