function fields = readViconCSV(csv_file)
%% Read the csv file containing the data

fid = fopen(csv_file);

%# Get file size.
fseek(fid, 0, 'eof');
file_size = ftell(fid);
frewind(fid);
%# Read the whole file.
whole_file = fread(fid, file_size, 'uint8');
%# Count number of line-feeds and increase by one.
number_lines = sum(whole_file == 10) + 1;

frewind(fid);

%Read the "Trajectory" line
fgetl(fid);

%Read the "100" line
fgetl(fid);

%Read the tags line
line = fgetl(fid);
tags = textscan(line,'%s','delimiter',',');
fields.tags = tags{1}';

%Read the column id line
line = fgetl(fid);
column_ids = textscan(line,'%s','delimiter',',');
fields.column_ids = column_ids{1}';

%Read the measurement unis line
line = fgetl(fid);
measurement_units = textscan(line,'%s','delimiter',',');
fields.measurement_units = measurement_units{1}';

%Get the number of usefull data lines
number_data_lines = number_lines - 5 -1;

fields.data = zeros(number_data_lines,length(fields.column_ids));

n = 0;
while 1
    n = n + 1;
    
    %Read data line
    line = fgetl(fid);

    %Check if valid, if not break and exit
    if ~ischar(line) || isempty(line), break, end
    
    %Read the data in the line
    data_line = textscan(line,'%f','delimiter',',');

    %Get matrix from cell array
    data_line_raw = data_line{1}';
    
    %Pad line with extra NaN at the end if needed
    data_line_padded = padarray(data_line{1}',[0 size(fields.data,2)-size(data_line_raw,2)],NaN,'post');
    
    %Store in the main matrix
    fields.data(n,:) = data_line_padded;
end
