% FUNCTION [h] = plotNineColumns(U)
%
% PURPOSE: Plot the first nine columns of U in a 3x3 grid of subplots.
%
% INPUTS: A matrix U with at least nine columns
%
% OUTPUTS: h, the handle to each of the nine subplots
%

function [h] = plotNineColumns(U)

[rows,cols] = size(U);
if cols < 9,
    error('Called plotNineColumns with matrix with < 9 columns');
end

% Add 20% to the maximum amplitude (and round to 2 decimal places) to set the y-axis upper limit.
maxAmpl = round(max(max(abs(U(:,1:9)))) * 1.2 * 100) / 100

% Go sequentially to each of the nine subplots.
for n=1:9,
    subplot(3,3,n)  % Make it a 3 by 3 grid
    h(n) = plot(U(:,n)); % plot one column
    set(h(n),'LineWidth',2)
    set(h(n),'Color','k')
    grid
    set(gca,'xlim',[1 rows])
    set(gca,'ylim',[-maxAmpl maxAmpl])
    set(gca,'FontSize',20)
    set(gca,'yTick',[-0.2 0 0.2]);
    % Only put the axis labels on the left-most and bottom-most subplots
    % (otherwise it is too crowded).
    if mod(n,3)==1,
        set(gca,'yTickLabel',[-0.2, 0, 0.2])
    else
        set(gca,'yTickLabel',[]);
    end    
    set(gca,'xTick',[1 rows/2 rows]);
    if n > 6,
        set(gca,'xTickLabel',[1 rows/2 rows])
    else
        set(gca,'xTickLabel',[])
    end        
end
subplot(3,3,4)
ylabel('Signal Value')
subplot(3,3,8)
xlabel('Index')
