% Code to plot simulation results from sh_cyl_differential
%% Plot Description:
%
% The plots below show the pressure inside the cylinder, as well as the force
% between the piston and the hard stop inside the cylinder.

% Copyright 2015 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t = simlog_sh_cyl_differential.Load_Spring.x.series.time;
simlog_cylhsF = simlog_sh_cyl_differential.Differential_Cylinder.hs.f.series.values('N');
simlog_cylpA = simlog_sh_cyl_differential.Differential_Cylinder.A.p.series.values('Pa');
simlog_cylpB = simlog_sh_cyl_differential.Differential_Cylinder.B.p.series.values('Pa');

% 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
legend({'Side A','Side B'},'Location','Best');
grid on
title('Cylinder Pressures')
ylabel('Pressure (Pa)')

simlog_handles(2) = subplot(2, 1, 2);
plot(simlog_t, simlog_cylhsF, 'LineWidth', 1);
grid on
title('Cylinder Hard Stop Force')
ylabel('Force (N)')
xlabel('Time (s)')

linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear simlog_t simlog_cylhsF simlog_cylpA simlog_cylpB simlog_handles
