% Code to plot simulation results from sh_cyl_closed_circuit
%% Plot Description:
%
% The plots below show the pressures inside the cylinder and the time-varying
% load.

% Copyright 2015 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t = simlog_sh_cyl_closed_circuit.Cylinder.A.p.series.time;
simlog_cylpA = simlog_sh_cyl_closed_circuit.Cylinder.A.p.series.values('Pa');
simlog_cylpB = simlog_sh_cyl_closed_circuit.Cylinder.B.p.series.values('Pa');
simlog_cylF = simlog_sh_cyl_closed_circuit.Force_Source.S.series.values('N');

% 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 Pressures')
ylabel('Pressure (Pa)')
legend({'Side A','Side B'},'Location','Best');
simlog_handles(2) = subplot(2, 1, 2);
plot(simlog_t, simlog_cylF, 'LineWidth', 1)
grid on
title('Load Force')
ylabel('Force (N)')
xlabel('Time (s)')

linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear simlog_t simlog_handles
clear simlog_cylF simlog_cylpA simlog_cylpB 
