% Code to plot simulation results from sh_cyl_differential
%% Plot Description:
%
% This plot shows the position of the piston (system output) and the
% position of the valve spool (system input).

% 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('h1_sh_cyl_differential', 'var') || ...
        ~isgraphics(h1_sh_cyl_differential, 'figure')
    h1_sh_cyl_differential = figure('Name', 'sh_cyl_differential');
end
figure(h1_sh_cyl_differential)
clf(h1_sh_cyl_differential)

% Get simulation results
simlog_t = simlog_sh_cyl_differential.Load_Spring.x.series.time;
simlog_cylPos = simlog_sh_cyl_differential.Load_Spring.x.series.values('m');
simlog_valveCmd = simlog_sh_cyl_differential.x3_Way_Valve.S.series.values('m');

% Plot results
simlog_handles(1) = subplot(2, 1, 1);
plot(simlog_t, simlog_cylPos, 'LineWidth', 1);
grid on
title('Piston Position')
ylabel('Position (m)')

simlog_handles(2) = subplot(2, 1, 2);
plot(simlog_t, simlog_valveCmd, 'LineWidth', 1);
grid on
title('Valve Spool Position')
ylabel('Position (m)')
xlabel('Time (s)')

linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear simlog_t simlog_cylPos simlog_valveCmd simlog_handles
