% Code to plot simulation results from sh_cyl_simple
%% Plot Description:
%
% The plots below show the pressures in the cylinder and the position of
% the piston.

% Copyright 2015 The MathWorks, Inc.

% Generate simulation results if they don't exist
if ~exist('simlog_sh_cyl_simple', 'var')
    sim('sh_cyl_simple')
end

% Reuse figure if it exists, else create new figure
if ~exist('h1_sh_cyl_simple', 'var') || ...
        ~isgraphics(h1_sh_cyl_simple, 'figure')
    h1_sh_cyl_simple = figure('Name', 'sh_cyl_simple');
end
figure(h1_sh_cyl_simple)
clf(h1_sh_cyl_simple)

% Get simulation results
simlog_t = simlog_sh_cyl_simple.Load_Spring.x.series.time;
simlog_cylpA = simlog_sh_cyl_simple.Cylinder.Piston_Side_A.A.p.series.values('Pa');
simlog_cylpB = simlog_sh_cyl_simple.Cylinder.Piston_Side_B.A.p.series.values('Pa');
simlog_cylx = simlog_sh_cyl_simple.Load_Spring.x.series.values('m');
simlog_cylc = simlog_sh_cyl_simple.Subtract.I1.series.values('m');

% Plot results
simlog_handles(1) = subplot(2, 1, 1);
plot(simlog_t, simlog_cylpA, 'LineWidth', 1)
hold on
plot(simlog_t, simlog_cylpB, 'LineWidth', 1)
hold off
grid on
title('Cylinder Pressure')
ylabel('Pressure (Pa)')
legend({'Side A','Side B'},'Location','Best');

simlog_handles(2) = subplot(2, 1, 2);
plot(simlog_t, simlog_cylx, 'LineWidth', 1)
hold on
plot(simlog_t, simlog_cylc, 'LineWidth', 1,'LineStyle','--')
hold off
grid on
title('Piston Position')
ylabel('Position (m)')
xlabel('Time (s)')
legend({'Measured','Commanded'},'Location','Best');

linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear simlog_t simlog_cylpA simlog_cylpB simlog_cylx simlog_cylc simlog_handles
