%
% This script plots data in the columns of a nx5 matrix A.
% It assumes that the fifth column contains 0s and 1s,
% which indicate "slow" or "fast".  The slow segments
% are plotted with blue, and the fast segments are plotted
% with red.
%
%
% -----------------------------------
% Create some test data...
% -----------------------------------
n = 500;
t = linspace(0,4*pi,n);
A = zeros(n,5);
A(:,1) = t .* cos(t);
A(:,2) = t .* sin(t);
A(:,3) = exp(-0.25*t) .* cos(t);
A(:,4) = exp(-0.25*t) .* sin(t);
A(:,5) = 0.5*(sign(sin(2.5*t+1))+1);
% -----------------------------------
%

clf
hold on

%
% xindex and yindex are the columns of A
% that should be used for the x and y axes,
% respectively.
% (With the test data computed above, using
% 1 and 2 plots a "spiral of Archimedes", and
% using 3 and 4 plots a "logarithmic spiral".)
%
xindex = 1;
yindex = 2;

%
% Find the row indices where the fifth column
% changes from 0 to 1 or 1 to 0.
% Then append the index of the last row
% of A (i.e. the number of rows) to ichange.
% This ensures that the last block gets
% plotted, and it also means the code still
% works when no changes are found.
%
ichange = find(A(1:end-1,5) ~= A(2:end,5));
ichange = [ichange; size(A,1)];

k = 1;
for j = ichange'
    %
    % Plot the data in rows k to j.
    % First set the color based on the
    % fifth column.
    %
    if (A(j,5) == 0)
        clr = 'b';
    else
        clr = 'r';
    end
    plot(A(k:j,xindex),A(k:j,yindex),clr,'linewidth',2);
    k = j;
end

