% Code to plot simulation results from sdl_detents_translational
%% Plot Description:
%
% The plot below shows the shear force applied by each detent to the mass.
% As the force applied exceeds shear force, the detent member is pushed
% into its hole and the mass can continue moving until it reaches the hard
% stop.

% Copyright 2016 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t = simlog_sdl_detents_translational.Hard_Stop.x.series.time;
simlog_xMass = simlog_sdl_detents_translational.Hard_Stop.x.series.values('mm');
simlog_fDetent(:,1) = simlog_sdl_detents_translational.Detent_1.F_shear.series.values('N');
simlog_fDetent(:,2) = simlog_sdl_detents_translational.Detent_2.F_shear.series.values('N');
simlog_fDetent(:,3) = simlog_sdl_detents_translational.Detent_3.F_shear.series.values('N');

% Plot results
%simlog_handles(1) = subplot(2, 1, 1);
yyaxis left
temp_h=plot(simlog_t, simlog_fDetent, 'LineWidth', 1);
set(temp_h(3),'LineStyle','-.');
ylabel('Shear Force (N)')
yyaxis right
temp_h=plot(simlog_t, simlog_xMass, 'LineWidth', 1);
grid on
title('Detent Shear Force and Mass Position')
ylabel('Position (m)')
legend({'Detent 1','Detent 2','Detent 3'},'Location','SouthEast');
xlabel('Time (s)')


% Remove temporary variables
clear simlog_t
clear simlog_fDetent simlog_xMass temp_h

