function [pltData, stats] = sortdata(data)
    date = dateshift(data.Time(1),'start','day');
    time_hms = data.Time-date;
    time_s = seconds(time_hms);
    time_s = time_s - time_s(1);    % set beginning of session to 0

    % x-Axis:
    pltData.time_s = time_s;
    pltData.time_hms = time_hms;


    % distance calculation
    speed_mps = movmean(data.ABX_Driver_ABX_Speed,50); % TODO: test different speed filters for distance calculation
    pltData.distance(1) = 0;
    for i = 2:length(speed_mps)
        pltData.distance(i) = speed_mps(i)*(time_s(i)-time_s(i-1)) + pltData.distance(i-1);
    end
    pltData.distance = pltData.distance.';
    pltData.xAxis = pltData.time_s;     % set time as default x-Axis for plotting

    %% Misc
    pltData.app_percent = data.ABX_Driver_ABX_APPS_percent;
    pltData.speed_kph = 3.6*movmean(data.ABX_Driver_ABX_Speed,50);  % same filter as for distance calculation ???
    pltData.steering_deg = data.ABX_Driver_ABX_Steering_Angle;

    %% AMS:
    pltData.ams_soc = data.AMS_Status_SOC;
    pltData.ams_tmax = data.AMS_Status_Max_cell_temp;
    pltData.ams_utot = data.Shunt_Voltage1_Shunt_Voltage1;
    pltData.ams_itot = data.Shunt_Current_Shunt_Current;
    % calculations:
    pltData.ams_ptot = pltData.ams_utot.*pltData.ams_itot/1000;

    %% Brakes
    % brake pressure
    pltData.brakePFront_bar = data.ABX_Driver_ABX_BrakeP_F;
    pltData.brakePRear_bar = data.ABX_Driver_ABX_BrakeP_R;
    % brake disc temperatures
    pltData.brakeTFrontLeft_degC = data.ABX_BrakeT_ABX_BrakeT_FL;
    pltData.brakeTFrontRight_degC = data.ABX_BrakeT_ABX_BrakeT_FR;
    pltData.brakeTRearLeft_degC = data.ABX_BrakeT_ABX_BrakeT_RL;
    pltData.brakeTRearRight_degC = data.ABX_BrakeT_ABX_BrakeT_RR;
    % calculate brake bias [%]
    minBrakeP = 5;  % minimum brake pressure to avoid artifacts due to sensor noise near 0 bar
    brakePFront = pltData.brakePFront_bar;
    brakePFront(brakePFront < minBrakeP) = minBrakeP;
    brakePRear = pltData.brakePRear_bar;
    brakePRear(brakePRear < minBrakeP) = minBrakeP;
    pltData.brakeBias_perc = 100*brakePFront./brakePRear;  % check calculation!

    %% Cooling system

    %% Dampers
    % damper positions
    pltData.damper_FL_mm = data.ABX_Dampers_ABX_Damper_FL; %Heave_F
    pltData.damper_FR_mm = data.ABX_Dampers_ABX_Damper_FR; %Roll_F
    pltData.damper_RL_mm = data.ABX_Dampers_ABX_Damper_RL; %Heave_R
    pltData.damper_RR_mm = data.ABX_Dampers_ABX_Damper_RR; %Roll_R
    % calculate damper velocities
    pltData.velocity_FL_mmps(1) = 0;
    pltData.velocity_FR_mmps(1) = 0;
    pltData.velocity_RL_mmps(1) = 0;
    pltData.velocity_RR_mmps(1) = 0;
    for i = 2:length(pltData.time_s)
        timestep = pltData.time_s(i)-pltData.time_s(i-1);
        pltData.velocity_FL_mmps(i) = pltData.damper_FL_mm(i)-pltData.damper_FL_mm(i-1)/timestep;
        pltData.velocity_FR_mmps(i) = pltData.damper_FR_mm(i)-pltData.damper_FR_mm(i-1)/timestep;
        pltData.velocity_RL_mmps(i) = pltData.damper_RL_mm(i)-pltData.damper_RL_mm(i-1)/timestep;
        pltData.velocity_RR_mmps(i) = pltData.damper_RR_mm(i)-pltData.damper_RR_mm(i-1)/timestep;
    end
    % filter damper velocities ??? bessere Berechnung über mittelwert aus mehreren werten? Vorfilterung?
    pltData.velocity_FL_mmps = movmean(pltData.velocity_FL_mmps,100).';
    pltData.velocity_FR_mmps = movmean(pltData.velocity_FR_mmps,100).';
    pltData.velocity_RL_mmps = movmean(pltData.velocity_RL_mmps,100).';
    pltData.velocity_RR_mmps = movmean(pltData.velocity_RR_mmps,100).';

    %% IMU - VN200, no need to implement didnt work well anyway
    % Acceleration
    pltData.acc_long_g = movmean(data.VN200_IMU_ACC_LIN_AccLinX,100)/9.81;
    pltData.acc_lat_g = movmean(data.VN200_IMU_ACC_LIN_AccLinY,100)/9.81;
    % Rate of turn
    pltData.rot_roll_degps = movmean(data.VN200_IMU_ACC_ANG_AccAngX,100);
    pltData.rot_pitch_degps = movmean(data.VN200_IMU_ACC_ANG_AccAngY,100);
    pltData.rot_yaw_degps = movmean(data.VN200_IMU_ACC_ANG_AccAngZ,100);

    %% Inverters
    % inverter temperatures
    pltData.invL_temp = data.INV_L_TxPDO_1_T_Inv_L;
    pltData.invR_temp = data.INV_R_TxPDO_1_T_Inv_R;
    % motor temperatures
    pltData.motL_temp = data.INV_L_TxPDO_1_T_Mot_L;
    pltData.motR_temp = data.INV_R_TxPDO_1_T_Mot_R;
    % motor velocities
    pltData.motL_vel_rpm = 60*data.INV_L_TxPDO_4_Velocity_L;
    pltData.motR_vel_rpm = 60*data.INV_R_TxPDO_4_Velocity_R;
    % inverter torque demand
    pltData.invL_torqueDemand = data.INV_L_TxPDO_3_DemandedTorque_L/10; % /10 to match autobox torque
    pltData.invR_torqueDemand = data.INV_R_TxPDO_3_DemandedTorque_R/10;
    % inverter actual torque
    pltData.invL_torqueActual = data.INV_L_TxPDO_3_ActualTorque_L/10;
    pltData.invR_torqueActual = data.INV_R_TxPDO_3_ActualTorque_R/10;

    %% Wheelspeed


    

    %% Statistics:
    power_regen_kw = pltData.ams_ptot;
    power_regen_kw(power_regen_kw > 0) = 0;
    power_used_kw = pltData.ams_ptot;
    power_used_kw(power_used_kw < 0) = 0;
    stats.energy_kwh = trapz(time_s, pltData.ams_ptot)/3600;
    stats.energy_regen_kwh = trapz(time_s, power_regen_kw)/3600;
    stats.energy_used_kwh = trapz(time_s, power_used_kw)/3600;
    stats.distanceTotal_km = trapz(time_s, pltData.speed_kph./3.6)/1000; % 1/3.6 for km/h to m/s and /1000 for km output
    stats.peakPower_kw = max(pltData.ams_ptot);
    stats.peakPowerMean_kw = max(movmean(pltData.ams_ptot,500));

    stats.maxSpeed_kph = max(pltData.speed_kph);
    % pltData.startTime = time_hms(1);
    % pltData.stopTime = time_hms(end);

    %% struct2timetable
    pltData = table2timetable(struct2table(pltData), "RowTimes", data.Time); 
end

