Skip to content

Instantly share code, notes, and snippets.

@dpkoch
Last active August 29, 2015 14:24
Show Gist options
  • Save dpkoch/c782045663806cb35d78 to your computer and use it in GitHub Desktop.
Save dpkoch/c782045663806cb35d78 to your computer and use it in GitHub Desktop.
Extracts data for a specified group from a PX4 log file
function group = extract_group(logfile, label)
%EXTRACT_GROUP Extracts data for the specified group from a PX4 log file
% EXTRACT_GROUP(LOGFILE,LABEL) extracts data for the group LABEL from the
% PX4 log file LOGFILE, and returns a struct whose fields are the members
% of that group. The returned struct also has a field TIME_StartTime
% containing the timestamps for the data.
index = find(logfile == '.', 1, 'last');
if isempty(index)
basename = logfile;
else
basename = logfile(1:index-1);
end
data_file = [basename '_' label '.csv'];
status = system(sprintf('python sdlog2_dump.py %s -f %s -m TIME -m %s',...
logfile, data_file, label));
if status ~= 0
error('Error parsing log file %s', label, logfile)
end
log = importdata(data_file);
if ~isstruct(log) || ~isfield(log, 'colheaders') || ~isfield(log, 'data')
error('Error reading data for group %s from log file %s', label, logfile)
end
label_format = [label '_%s'];
labels = cell(1, length(log.colheaders));
data = cell(1, length(log.colheaders));
for i=1:length(log.colheaders)
if strcmp(log.colheaders{i}, 'TIME_StartTime')
labels{i} = 'TIME_StartTime';
else
labels{i} = sscanf(log.colheaders{i}, label_format);
end
data{i} = log.data(:,i);
end
group = cell2struct(data, labels, 2);
end
@dpkoch
Copy link
Author

dpkoch commented Jul 14, 2015

Note that the extraction script sdlog2_dump.py must be in the current directory

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