Code covered by the BSD License  

Highlights from
Load Tobii eyetracker TSV (export) data files

Load Tobii eyetracker TSV (export) data files

by

 

22 May 2012 (Updated )

This function load a TSV file generated by the Tobii Studio software.

loadTobiiTSV(fileName, chosenColumns, nbHeaderLines, dataFormat)
function dataFile = loadTobiiTSV(fileName, chosenColumns, nbHeaderLines, dataFormat)
%This fucntion load a TSV file generated by the Tobii Studio software. It
%should be generated with the default option "All data".
% IN:
%   fileName: the full parth and name of the file to load
%   chosenColumns: a cell array of strings containing the list of columns labels to
%                  keep in the data. Can be a string for a single column.
%                  If an empty array is given then no columns are extracted
%                  and only the header is returned in the output.
%                  The string 'All' indicates that all column should be
%                  taken (default: 'All')
%   nbHeaderLines: the number of lines to skip before finding the columns
%                  with Tobii data (default: 41)
%   dataFormat: the format of the columns as in the texscan matlab
%               function. (default: '%u%s%s%u%f ...')
% OUT:
%   data: a structure containing the content of the tobii file. The structure
%   has the following fields:
%       - header [Hx?]: the H first lines of the file in text format (can include the list
%                       of the column headers as the last line)
%       - colname {1xC}: a cell array of strings containing the column
%                        headers of the chosen columns
%       - data {1xC}: a cell array containing the data of each column. Each
%                     cell contain a cell array (for strings) and a vector
%                     for other data formats of size (NbSamplesx1)
%
% Author: Guillaume Chanel
% Date: 22.05.2012

%input checks
if(nargin < 2)
    chosenColumns = 'All';
end
if(nargin < 3)
    nbHeaderLines = 41;
end
if(nargin < 4)
    dataFormat = '%u%s%s%u%f%f%f%f%f%f%u%f%f%f%f%f%f%u%u%d%d%s%s%s%s%s%s%d%d%d%d%d%d%d%d%d%s%s%d%d%d%d64%d64%f%f%f%f%f%f%f%f%f%f%f%f';
end
if(ischar(chosenColumns))
    chosenColumns = {chosenColumns};
end
   
    
%Compute the number of expected columns according to the format string
nbExpectedCol = sum(dataFormat == '%');

%Open the file and read the header
[file errMessage] = fopen(fileName);
if(~isempty(errMessage))
    error('Chanel:CannotOpenFile', ['Cannot open input file ' fileName ': ' errMessage]);
end

dataFile.header =[];
for(iL=1:nbHeaderLines)
    line = fgetl(file);
    dataFile.header = strvcat(dataFile.header,line);
end

%Stop there if no columns are requested
if(isempty(chosenColumns))
    fclose(file);
    return;
end

%The last read file contains the list of column names let's process it
%correctly and put it in a nice cell array
sep = sprintf('\t');
nbCols = sum(line==sep);
if(nbCols ~= nbExpectedCol)
    fclose(file);
    error('Chanel:BadExpectations', ['The expected number of columns is ' num2str(nbExpectedCol) ' and ' num2str(nbCols) ' were found in the file']);    
end
dataFile.colNames = textscan(line,repmat('%s',1,nbCols),1,'delimiter',sprintf('\t'));
dataFile.colNames = cellfun(@cellstr,dataFile.colNames); %flatten to avoid multiple cells in cells


%In the case where not all columns are requested
%Find the requested columns and modify the dataFormat accordingly (to skip the proper columns)
if(~strcmp('All', chosenColumns))
    %Find the index of the chosen columns
    selectedCols = false(1, nbCols);
    for(iC=1:length(chosenColumns))
        selectedCols = selectedCols | strcmp(chosenColumns{iC}, dataFile.colNames);
    end
    
    %Update the column names
    dataFile.colNames = dataFile.colNames(selectedCols);
    
    %Get the dataFormat and separate each format in a cell
    separatedDataFormat = textscan(dataFormat(2:end), '%s', 'delimiter', '%');
    separatedDataFormat = separatedDataFormat{1}; %from that point the variable contains the list of data type in a cell array: {'u' 's' 's' ... }
    
    %Reformat the dataFormat with the * to skip them
    separatedDataFormat(~selectedCols) = strcat('*', separatedDataFormat(~selectedCols));
    separatedDataFormat = strcat('%', separatedDataFormat);
    dataFormat = strcat(separatedDataFormat{:});
end

%Load the rest of the data in a cell array and return the whole set of data
dataFile.data = textscan(file,dataFormat,'delimiter',sprintf('\t'));

%close the file
fclose(file);

Contact us