function [pltData,laps_strArray] = loadvector(filepath) % Load dataset: fprintf("Loading: %s\n",filepath) data = vector2timetable(filepath,"ABX*","Shunt*","AMS_Status*","INV*","XSens*"); fprintf("Finished loading\n------------------------\n") % get date / time date = dateshift(data.Time(1),'start','day'); fprintf("Date: %s\n",date) fprintf("Start: %d:%d:%02.0f\n",hour(data.Time(1)),minute(data.Time(1)),second(data.Time(1))) fprintf("End: %d:%d:%02.0f\n",hour(data.Time(end)),minute(data.Time(end)),second(data.Time(end))) fprintf("------------------------\n") % get lapcounter / laptrigger laps_counter = data.ABX_Driver_ABX_Lapcounter; timestamp_hms(1) = data.Time(1) - date; timestamp_index(1) = 1; j = 2; for i = 2:length(laps_counter) if laps_counter(i) ~= laps_counter(i-1) timestamp_hms(j) = data.Time(i) - date; timestamp_index(j) = i; j = j + 1; end end % calculate laptimes timestamp_s = seconds(timestamp_hms); for i = 1:length(timestamp_s)-1 laps_s(i) = timestamp_s(i+1) - timestamp_s(i); end % if laps could be detected find fastest lap and create list of % laptimes, otherwise only "Entire stint" will be selectable laps_strArray(1) = sprintf("Entire stint"); if (exist("laps_s","var") == true) % create list of laptimes for i = 1:length(laps_s) if laps_s(i) == min(laps_s) laps_strArray(i+1) = sprintf("Lap %d: %.3fs (best)",i,laps_s(i)); else laps_strArray(i+1) = sprintf("Lap %d: %.3fs",i,laps_s(i)); end end % display fastest lap [laps_best_time, laps_best] = min(laps_s); fprintf("Fastest lap: %.2fs (%d)\n",laps_best_time,laps_best) end % create time arrays in hh:mm:ss and just seconds time_hms = data.Time-date; time_s = seconds(time_hms); time_s = time_s - time_s(1); % set beginning of session to 0 % calcualte energy consumption and total distance power_kw = data.Shunt_Voltage1_Shunt_Voltage1.*data.Shunt_Current_Shunt_Current/1000; power_regen_kw = power_kw; power_regen_kw(power_regen_kw > 0) = 0; power_used_kw = power_kw; power_used_kw(power_used_kw < 0) = 0; energy_kwh = trapz(time_s, power_kw)/3600; energy_regen_kwh = trapz(time_s, power_regen_kw)/3600; energy_used_kwh = trapz(time_s, power_used_kw)/3600; fprintf("Energy used: %.2fkWh\n",energy_used_kwh) fprintf("Energy regen: %.2fkWh\n",energy_regen_kwh) fprintf("Energy total: %.2fkWh\n",energy_kwh) distance_km = trapz(time_s, movmean(data.ABX_Driver_ABX_Speed,10))/1000; fprintf("Distance driven: %.2fkm\n",distance_km) fprintf("------------------------\n") pltData(1) = sortdata(data,1,length(data.Time)); % sort data for plotting: for i = 1:length(timestamp_index)-1 pltData(i+1) = sortdata(data,timestamp_index(i),timestamp_index(i+1)); end end