function h = plotExperiment(m, sim, t, varargin)
% PLOTEXPERIMENT plots a simulation. 
%
% This function plots a simulation generated by simulate.
%
% h = subplotExperiment(m, sim) plots the output of the simulation
%       generated by simulate.
%
% h = subplotExperiment(m, sim, t) plots the output of the simulation
%       generated by simulate at the given times. 
%
% h = subplotExperiment(m, sim, t, ...) additional arguments are passed to
%       the plot command. For example:
%           plotExperiment(m, sim, t, 'd:');
%       will results in dotted lines with diamond markers at the time
%       points where the simution was evaluated 
%
% Arguments:
% m     The model that was simulated
% sim   The simulation returned from simulate
% t     The time interval to be plotted
%       If t is a scalar then simution will be plotted from time 0 to t
%       If t is length > 1 then the simution will be ploted only at times t
% h     A column vector of handles to lineseries graphics objects,
%       one handle per line
% 
% see also: simulate, plotExperiment, plot

% This work is licensed under the Creative Commons Attribution-Noncommercial-No Derivative 
% Works 3.0 United States License. To view a copy of this license, visit 
% http://creativecommons.org/licenses/by-nc-nd/3.0/us/ or send a letter to Creative Commons,
% 171 Second Street, Suite 300, San Francisco, California, 94105, USA.

if(nargin == 2)
    tPlot = sim.t;
elseif(length(t) == 1)
    tPlot = [sim.t(sim.t<t), t];
else
    tPlot = t;
end
   
yPlot = sim.y(tPlot);
h     = plot(tPlot, yPlot, varargin{:}, 'LineWidth', 2);
xlabel('Time');
ylabel('Concentration');
if isfield(m, 'name');
    title(m.name);
end
legend(m.yNames);