clear all
close all

hFigure = figure;
set(hFigure,'DoubleBuffer', 'on');

hAxes = axes();
% maintain X tick spacing
ticks = get(hAxes,'XTick');
dx = mean(diff(ticks));

% enforce right justification for grid lines & remove X tick labels
% range = get(ax,'XLim');
% if range(end)~=ticks(end)
%   ticks = fliplr(range(end):-dx:range(1));
%   set(ax,'XTick',ticks)
% end
% set(ax,'XTickLabel',[], 'XTickMode', 'Manual', ...
%             'XGrid', 'On', 'YGrid', 'On', ...
%             'XLimMode', 'manual');

startTime = now;
index = 1;

arrayLength = 1000;
time = NaN * ones(1,arrayLength);
y = NaN * ones(1,arrayLength);

SCWidth = 100;

plotX = time(end-SCWidth + 1:end);
plotY = y(end-SCWidth+1:end);
hPlot = plot(plotX, plotY);
grid;

for i = 1:200
    time(1:arrayLength-1) = time(2:end);
    y(1:arrayLength-1) = y(2:end);
    time(arrayLength) = (now - startTime) * 24.0 * 3600.0;
    %y(arrayLength) = 10 * (rand() - 0.5);
    y(arrayLength) = sin(time(arrayLength));

    plotX = time(end-SCWidth + 1:end);
    plotY = y(end-SCWidth+1:end);

    set(hPlot, 'XData', plotX, 'YData', plotY);
    set(hAxes, 'XLim', [(time(end) - 10) time(end)]);
    drawnow
    
    pause(0.2);
end
    
    
    