function plot_time_histories(fig_num,t,x,ylabel_str,main_title)

[nr,nc] = size(x);

if nc < 1 || nc > 4
    error('Amplitude array must have between 1 and 4 columns.');
end

%% Figure size scaling
fig_width = 900;
height_per_plot = 180;
fig_height = height_per_plot * nc;

%% Center figure
scr = get(0,'ScreenSize');
left   = (scr(3)-fig_width)/2;
bottom = (scr(4)-fig_height)/2;

figure(fig_num);
clf
set(gcf,'Position',[left bottom fig_width fig_height])

%% Determine neat symmetric limits
yabs = max(abs(x(:)));

[ymax,dy] = nice_axis_limits(yabs);

%% Layout
tl = tiledlayout(nc,1);
tl.TileSpacing = 'compact';
tl.Padding = 'compact';

ax = gobjects(nc,1);

for i = 1:nc
    
    ax(i) = nexttile;
    
    plot(t,x(:,i),'LineWidth',0.9)
    
    grid on
    grid minor
    
    set(gca,'MinorGridLineStyle',':')
    
    ylabel(ylabel_str)
    
    if nc > 1
        title(sprintf('dof %d',i))
    end
    
    ylim([-ymax ymax])
    
    yticks(-ymax:dy:ymax)
    
    if i ~= nc
        set(gca,'XTickLabel',[])
    else
        xlabel('Time (sec)')
    end
    
end

%% Consistent x limits
xmin = min(t);
xmax = max(t);

for i = 1:nc
    xlim(ax(i),[xmin xmax])
end

%% Link x axes
if nc > 1
    linkaxes(ax,'x')
end

%% Main title
title(tl,main_title)

set(ax,'FontSize',11)

end


%% ----------------------------------------------------------
function [ymax,dy] = nice_axis_limits(yabs)

target_ticks = 6;   % ideal number of ticks

raw_step = yabs / (target_ticks/2);

pow = 10^floor(log10(raw_step));

base = raw_step / pow;

if base <= 1
    nice = 1;
elseif base <= 2
    nice = 2;
elseif base <= 5
    nice = 5;
else
    nice = 10;
end

dy = nice * pow;

ymax = ceil(yabs/dy)*dy;

end