Use matlab to process INCF acquisition data, mdf (.dat) format files, and write them into excel files

Use matlab to process INCF acquisition data, mdf (.dat) format files, and write them into excel files

Recently, I am working on the data of a certain car company on car calibration. The data is collected by INCA and saved in .dat format. The data can be graphically displayed by measure data analyzer and exported to other formats, but the software download is troublesome. It is also very convenient to use matlab to process.

Data processing steps

1. Read the file

// 添加项目路径到matlab中
folder ='要操作的文件路径';  % 要操作的文件夹
addpath( genpath(folder) ); 

2. Use the mdf function to access the .dat file

filename = '想要读取的文件名.dat';
mdfobj = mdf(filename);

After reading, you can view the content of the mdfobj variable in the Workspace area of ​​matlab, as follows, where
insert image description here
ChannelNames is the characteristic name of the data, but due to the different frequency of data sampling, the data is divided into 8 cells, and the sampling frequency in each cell same features.

3. The next step is how to read the value under the feature name in each cell. Due to the different data format and naming style, use a function to read the data

%该函数用于查找mdf文件中的变量,并输出数据和对应的时间
function[data,time] = data_time_output(Variable,mdfobj)
    i=1;
    ChannelLength=length(mdfobj.ChannelNames);
    for i=1:ChannelLength
        PositionVariableLogic=strcmp(mdfobj.ChannelNames{
    
    i},Variable);
        %在频道i中查找对应的Variable,并输出该频道中的每个cell是否含有该Variable对应的逻辑值,如果这个频道中有该Variable,则输出1,如没有则输出0
        PositionVariable=find(PositionVariableLogic);
        %FINDposition,找到对应的位置
        if PositionVariable>0
            [data, time] = read(mdfobj, i, Variable, 1, 1000000000, 'OutputFormat', 'vector');
        else 
            continue
        end
    end
end

4. Call the function and read the data

//Variable是想要读取的特征
Variable = 'C_3_0_217\CAN-Monitoring: 1';
[data,time] = data_time_output(Variable, mdfobj);
varNames = {
    
    Variable};

5. Write the read data into excel

//table函数将特征名和数值组合成同一列
T = table(data,'VariableNames',varNames);
writetable(T, '2021-11-18_09.31.59_k1_valve_dither_test_20.0bar_100Hz_250mA.csv','WriteRowNames',true)

6. Generally, there are multiple features in a cell. At this time, it is necessary to read all the data in the cell once in a cycle.

//程序中数字1为第一个cell
for i=1:length(mdfobj.ChannelNames{
    
    1})
    Variable = mdfobj.ChannelNames{
    
    1}{
    
    i};
    [data,time] = data_time_output(Variable, mdfobj);
`	varNames = {
    
    Variable};
    T = table(data,'VariableNames',varNames);
    H(:,i)=T; //这个地方如果不用H只能将cell中最后一列写入表中,暂时没有好的办法,如果有人知道请留言,谢谢
end
writetable(H, '想要写入的表名.xlsx','WriteMode','Append','WriteRowNames',true)

For more usage of the table and writetable functions, please refer to the matlab help documentation


Guess you like

Origin blog.csdn.net/weixin_40061485/article/details/123141423