Skip to content

Instantly share code, notes, and snippets.

@billyzs
Last active September 23, 2016 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billyzs/c777849541e20c333224a6e46231fb0c to your computer and use it in GitHub Desktop.
Save billyzs/c777849541e20c333224a6e46231fb0c to your computer and use it in GitHub Desktop.
MATLAB helper function to load IMU data into matrices
function [ imu_data, gps_data ] = load_imu_data( file_name )
%load data from txt file containing IMU output
%written by Shao Zhou billyzs.728[at] gmail.com
% imu_data is n * 13 matrix that contains data fileds in the following sequence:
% Time
% Acceleration (x, y, z) in m/s
% Angular Velocity (p, q, r) in rad/s
% Magnetic field strength (x, y, z) in muT
% Roll, Pitch, Yaw in deg
% imu_data is m * 6 matrix that contains data fileds in the following sequence:
% Time
% GPS Lat in DDMM.MMMM
% GPS Long in DDMM.MMMM
% GPS Altitude in cm
% GPS Speed in knots
% GPS Heading
l = sum(fileread(file_name) == 10) - 1; % count the number of data lines in the file by counting how many ASCII #10 (EOL) markers
id = fopen(file_name);
% imu data:
imu_data = csvread(file_name, 1, 0, [1, 0, l+1, 12]);
fclose(id);
%gps data:
gps_data = zeros(l, 6);
id = fopen(file_name, 'r');
format_spec = '%f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %d';
n = 1;
c = 1;
line = fgetl(id);
while n <= l
line = fgetl(id);
if sum(line == ',') == 18 % if line contains gps data
m = textscan(line, format_spec);
m = cell2mat(m(1,1:18));
gps_data(c, :) = [m(1,1), m(1, 14:18)];
c = c + 1;
end
n = n+1;
end
gps_data = gps_data(1:c-1, :);
fclose(id);
end
% make sure txt file is in the same folder as this script
[imu_data, gps_data] = load_imu_data('your_file.txt');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment