function phasespaceplot(phaseSpace, lag, dim, numTS, index)
  % PHASESPACEPLOT Phase space plot to be used in conjunction with the Phase
  % Space Reconstruction App.
  
  % Copyright 2018 The MathWorks, Inc.
  
  if istimetable(phaseSpace)
    phaseSpace = phaseSpace.Variables;
  end
  
  if nargin == 4
    predmaintguis.internal.plot.phaseSpacePlot(phaseSpace, lag, dim, numTS);
    return
  end
  
  nc = size(phaseSpace, 2);
  [c1,c2] = ind2sub([nc nc], index);
  
  h = newplot;
  set(h,'NextPlot','replace');
  ax = gca;
  
  if (c1 == c2)
    histogram(ax, phaseSpace(:,c1))
  else
    plot(ax, phaseSpace(:,c1), phaseSpace(:,c2), '.')
  end
  
  % title and info text on axes
  title(m('predmaint:plot:phaseSpacePlotTitle'));
  text(ax, 0.5, 0.96, ...
    sprintf('%s: %d, %s: %d', m('predmaint:embedded_apps:strLag'), lag, m('predmaint:embedded_apps:strDim'), dim), ...
    'Units', 'normalized', 'BackgroundColor', [1 1 1], ...
    'VerticalAlignment', 'top', 'HorizontalAlignment', 'center');
  
  % x label: e.g., x1(t+10)
  l1 = rem(c1-1,dim)*lag;
  d1 = ceil(c1/dim);
  if l1 == 0
    xlabel(ax, sprintf('x_%d(t)', d1), 'FontSize', 11);
  else
    xlabel(ax, sprintf('x_%d(t+%d)', d1, l1), 'FontSize', 11);
  end
  
  % y label: e.g., x1(t+10)
  l2 = rem(c2-1,dim)*lag;
  d2 = ceil(c2/dim);
  if l2 == 0
    ylabel(ax, sprintf('x_%d(t)', d2), 'FontSize', 11);
  else
    ylabel(ax, sprintf('x_%d(t+%d)', d2, l2), 'FontSize', 11);
  end
  
  % Overwrite y label for histograms
  if (c1 == c2)
    ylabel(ax, m('predmaint:embedded_apps:strFrequency'), 'FontSize', 11);
  end
end

function s = m(id, varargin)
  % Reads strings from the resource bundle
  s = string(message(id, varargin{:}));
end
