% Plot the data contained in the columns of a file.
%
% The file should be of the form:
%   time1 rss1 time2 rss2 .... timeN rssN
% where timex and rssx are the x and y data for stream x.
% Other options:
%   - The first startSkip lines are skipped.
%   - A buffer of data of length buffL is kept in memory and plotted.
%   - The plot is only refreshed every startSkip rows of the data file.
%
% Author: Neal Patwari, 30 Aug 2012
% Version 1.0:  Initial Release
%
% Version 1.1:  26 Sept 2012.
%   Change: Values of 127 (the no measurement key) are now NOT being appended 
%           to the vector of values to plot.
%

%

% Set this to the full path and file name for the data file.
dataFile = 'temp.txt'

% Parameters you may change:
%   plotSkip:  Refresh the plot after this many data lines are read
%   buffL:     buffer length, ie, how much data to plot.
%   startSkip: A serial port seems to have a "memory" of several lines, 
%              which were saved from the previous experiment run.  
%              ** Must be greater than 0.
plotSkip    = 3
buffL       = 80
startSkip   = 3

% Open the file and read startSkip lines to find # of streams.

FID     = fopen(dataFile,'r');
if FID < 0,
    disp(['File ' dataFile ' not able to be opened']);
end
i = 0;
while i < startSkip,
    line = fgetl(FID);
    if ischar(line),
        i = i + 1;
    end
end    
lineInt = strread(line,'%d','delimiter',' ');
columns = length(lineInt); 
streams = columns/2;  % There are two columns (time, rss) for each stream.
rss     = lineInt(2:2:end);  % take even number columns
times   = lineInt(1:2:end);  % take odd number columns
disp(['Data File contains ' num2str(streams) ' streams']);

% Init buffers
TimeBuffer = times * ones(1,buffL);
RSSBuffer  = rss * ones(1,buffL);

% Repeatedly read lines from the file.
counter = 0;
while (1),
    line = fgetl(FID);  % Get one line (row) from the file.
    
    % For real-time operation, line==-1 means that there isn't currently
    % a new line in the file.  But there may be one soon, so don't end.
    if ischar(line),
        
        % Get the integers from the line string
        lineInt = strread(line,'%d','delimiter',' ');
        rss     = lineInt(2:2:end);  % take even number columns
        times   = lineInt(1:2:end);  % take odd number columns
    
        % data == 127 indicates no data measured.  Don't include a new value.
        for k=1:length(rss),
            if (rss<127),   % the no measurement key is 127
                RSSBuffer(k,:)  = [RSSBuffer(k,2:buffL), rss(k)];
                TimeBuffer(k,:) = [TimeBuffer(k,2:buffL), times(k)];
            end
        end    
        % Every plotSkip rows, redraw the plot.  Time stamps, relative to the 
        %   maximum time stamp, are on the x-axis.
        counter = counter + 1;
        if mod(counter, plotSkip) == 0,
            relTime = TimeBuffer - max(max(TimeBuffer));
            mintime = min(min(TimeBuffer));
            plot(relTime', RSSBuffer');
            set(gca,'ylim',[-95 -25])
            drawnow
            % Pause?  Uncomment this if reading from a saved file.
            % Leave commented out if for a file being read in real time.
            %pause(0.1);  
        end
    end
end
