Skip to content

Instantly share code, notes, and snippets.

@JZfi
Created May 30, 2015 15:50
Show Gist options
  • Save JZfi/6eb7e64ebead7dffa3dc to your computer and use it in GitHub Desktop.
Save JZfi/6eb7e64ebead7dffa3dc to your computer and use it in GitHub Desktop.
Affectiva Q sensor support for Ledalab
function [time, conductance, event] = getAffectivaQcsv(fullpathname)
% Import Affectiva Q sensor data from exported headerless csv-files
% v1 by Niklas Juslin
% Headerless csv columns: Time,Z-axis,Y-axis,X-axis,Battery,∞Celsius,EDA(uS),Event
fid = fopen(fullpathname);
data = textscan(fid, '%s %f %f %f %f %f %f %u', 'HeaderLines',0, 'Delimiter',',');
fclose(fid);
conductance = data{1,7};
% Convert the timestamps to relative time in seconds for Ledalab
time = zeros(length(data{1,1}),1);
start = timestamp2sec(data{1,1}{1,1});
time(1) = 0;
eventId = 0;
event = [];
for line = 1:length(data{1,1});
% Convert timestamp to seconds relative to start
time(line) = timestamp2sec(data{1,1}{line,1}) - start;
% Add an event if event column is set (marked on device, the value is always 1 in this case)
if data{1,8}(line) > 0
eventId = eventId + 1;
event(eventId).time = time(line);
event(eventId).nid = eventId;
event(eventId).userdata = 'Marked on Device';
end
end
% Optionally reads additional marker data from another csv file (prompted)
% The dialog suggests a file fullpathname + 'm.csv'.
% The Q software forces a .csv ending to the given filename and the
% following trick makes it easier to name the files accordingly:
% Select the exported datafile, append 'm' to the filename and click save.
% Now you'll have a datafile.csvm.csv file.
default_markerfile = strjoin({fullpathname, 'm.csv'},'');
[filename, pathname] = uigetfile({'*.csv','CSV files'; '*.*','All files'}, 'Read markers from file (optional)', default_markerfile);
if ~isequal(filename, 0)
fid = fopen(fullfile(pathname, filename));
% Marker csv columns: Dataset,Marked on Device,Note,Start,End
% Lines with the word Dataset are ignored (treated as comments)
markers = textscan(fid, '%s %s %s %s %s', 'HeaderLines',0, 'Delimiter',',', 'CommentStyle','Dataset');
fclose(fid);
markedOnDeviceId = 0;
for line = 1:length(markers{1,4})
% Marked on device -> the event is already added above
if strcmp(markers{1,2}{line}, 'TRUE')
markedOnDeviceId = markedOnDeviceId + 1;
event(markedOnDeviceId).name = strrep(markers{1,3}{line}, '"', '');
% New marker added with the Q software (lacks a data row with Event col set as 1)
else
eventId = eventId + 1;
event(eventId).time = timestamp2sec(markers{1,4}{line}) - start;
event(eventId).nid = line;
event(eventId).name = strrep(markers{1,3}{line}, '"', '');
end
end
end
% Converts HH:mm:ss.nnn time format to seconds
function [time] = timestamp2sec(timestamp)
x = textscan(timestamp, '%f:%f:%f');
time = x{1} * 60 * 60 + x{2} * 60 + x{3};
@lukegood712
Copy link

I'm a bit confused about how to include this in running data through Ledalab. What should this output for me? I don't see the utility of a .csvm.csv file if Ledalab doesn't support that either. Please correct me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment