%% Decode CAN Data from BLF-Files
% This example shows you how to import and decode CAN data from BLF-files in MATLAB for analysis. The BLF-file used in this example was generated from Vector CANoe™ using the "CAN - General System Configuration (CAN)" sample. This example also uses the CAN database file, PowerTrain_BLF.dbc, provided with the Vector sample configuration.
%
%% Open the DBC-File
% Open the database file describing the source CAN network using the canDatabase function.

canDB = canDatabase("PowerTrain_BLF.dbc")

%
%% Investigate the BLF-File
% Retrieve and view information about the BLF-File. The blfinfo function parses general information about the format and contents of the Vector Binary Logging Format BLF-file and returns the information as a structure.

binf = blfinfo("Logging_BLF.blf")
binf.ChannelList

%
%% Read Data from BLF-File
% The data of interest was logged from the powertrain bus which is stored in channel 2 of the BLF-file. Read the CAN data using the blfread function. You can also provide the DBC-file to the function call which will enable message name lookup and signal value decoding.

blfData = blfread("Logging_BLF.blf", 2, "Database", canDB)

% View signals from an "EngineData" message.

blfData.Signals{3}

%
%% Repackage and Visualize Signal Values of Interest
% Use the canSignalTimetable function to repackage signal data from each unique message on the bus into a signal timetable. This example creates three individual signal timetables for the three messages of interest, "ABSdata", "EngineData" and "GearBoxInfo", from the CAN message timetable.

signalTimetable1 = canSignalTimetable(blfData, "ABSdata")
signalTimetable2 = canSignalTimetable(blfData, "EngineData")
signalTimetable3 = canSignalTimetable(blfData, "GearBoxInfo")

% To visualize the signals of interest, columns from the signal timetables can be plotted over time for further analysis.

subplot(3, 1, 1)
plot(signalTimetable1.Time, signalTimetable1.CarSpeed, "r")
title("{\itCarSpeed} Signal from {\itABSdata} Message", "FontWeight", "bold")
xlabel("Timestamp")
ylabel("Car Speed")
subplot(3, 1, 2)
plot(signalTimetable2.Time, signalTimetable2.EngSpeed, "b")
title("{\itEngSpeed} Signal from {\itEngData} Message", "FontWeight", "bold")
xlabel("Timestamp")
ylabel("Engine Speed")
subplot(3, 1, 3)
plot(signalTimetable3.Time, signalTimetable3.Gear, "y")
title("{\itGear} Signal from {\itGearBoxInfo} Message", "FontWeight", "bold")
xlabel("Timestamp")
ylabel("Gear")

%
% Copyright 2020-2021 The MathWorks, Inc.