


%-----------------------PARSE A FILE----------------------------------
% To parse a specific file 'file_name.txt', we first open the file, then we
% specify what data type to read from each column of the file using the
% command textscan, and finally we close the file.

% Open the file 'Syslog_data.txt' and define a unique id for this file (fid).
fid = fopen('Syslog_data.txt');

% Read from the file using the command textscan. This command takes two
% input attributes, the id of the file to process and a string describing
% the data types of each column in the file. %d corresponds to integers, %s
% to strings. The syntax is similar to C for reading from a file. The
% result of the textscan command is a cell array which contains in its
% elements the columns of the txt file.
C = textscan(fid, '%d %d %d %s %s %s %s %s %s %s %s %s %s %s %s %s');

% If we want to ignore some lines at the beginning of the file 
% (e.g., attribute names), we can use the following syntax:
C = textscan(fid, '%d %d %d %s %s %s %s %s %s %s %s %s %s %s %s %s', 'headerLines', 1);
% The two last parameters mean that we ignore the first lines of the file.

% If we want to ignore all data after a specific column we use the syntax:
C = textscan(fid, '%d %d %d %s %s %*[^\n]');
% With the previous command, we read the first six columns that correspond
% to %d %f %s %s %s %d and ignore all the rest (%*[^\n] means that we
% ignore everything else until the newline character).

% close the file
fclose(fid);

%-----------------------PROCESS THE DATA FROM THE FILE-------------------

% The second element of the cell array C (C{2}) contains the second column from
% the file Syslog_data.txt which is the id of the AP in which an event
% occurs. To find all unique AP ids that appear in C{2}, we use the command
% unique.
unique_APs = unique(C{2});

% If we want to apply Boolean indexing techniques on a cell array that contains 
% strings, we should first map the different strings to different integer ids. 
% Then we can construct a vector which has in each entry the integer id of the 
% corresponding string. Finally, we can apply boolean indexing techniques using 
% this vector instead of the cell array of strings. This procedure is implemented 
% by the command unique. The input parameter is the cell array of strings we want to process. 
% The first output ev_types contains all the unique strings that appear in the input
% cell array. The last output ev_id contains the vector with the integer ids
% that correspond to all strings that appear in the call array. 
[ev_types b ev_id] = unique(C{5});

% Find the integer id that corresponds to the event 'Associated'.
for k=1:1:size(ev_types, 1)
    if strcmp(ev_types(k), 'Associated') == 1
        association_ev_id= k;
    end
end

% Find the lines that correspond to the event 'Associated'.
lines = ev_id == association_ev_id;

% Find the timestamp that corresponds to the event 'Associated'.
times = C{1}(lines);

%----------------------PLOT 2D FIGURES-------------------------------------

% Consider that you want to plot a curve that connects a set of points C.
% First, you define the x coordinates of the points in C and save them in a
% vector x. Then you define then y coordinates of the points in C and save
% them in a vector y. For example, if we want to plot the sinus function,
% we first define a number of points for this function (samples). We choose
% the x coordinates for the samples and store them in the vector x and then
% we compute the value of the sinus function on these samples and store them
% in the vector y.
x = 0:2*pi/10000:2*pi;
y = sin(0:2*pi/10000:2*pi);

% We construct the plot using the matlab command plot. The first input
% parameter is the vector x and the second parameter is the vector y.
plot(x, y);

% Define the title of the plot.
title('Sinus function');

% Define a label for the x axis.
xlabel ('x');

% Define a label for the y axis.
xlabel('sin(x)');

% Define the limits of the x axis.
xlim([0 2*pi]);

% Define the limits of the y axis.
ylim([0 1]);
