main content

解码并将 can 数据写入 mdf 文件 -凯发k8网页登录

此示例说明如何在 matlab® 中从 mdf 文件导入 can 数据并对其进行解码以供分析,然后将解码后的数据写回到同一个文件中。

此示例中使用的 mdf 文件是使用 vector 的 canoe™ 基于 can - general system configuration (can) 采样生成的。此示例还使用 vector 采样配置提供的 can 数据库文件 powertrain.dbc

查看 mdf 文件的详细信息

使用 函数查看 mdf 文件的元数据。

mdfinfo("logging_mdf.mf4")
ans = 
  mdfinfo with properties:
   file details
                  name: "logging_mdf.mf4"
                  path: "/tmp/bdoc23a_2181783_2332753/tp3fb84b34/vnt-ex42187575/logging_mdf.mf4"
                author: ""
            department: ""
               project: ""
               subject: ""
               comment: ""
               version: "4.10"
      initialtimestamp: 2020-06-25 20:41:13.133000000
   creator details
     programidentifier: "mdf4lib"
     creatorvendorname: "vector informatik gmbh"
       creatortoolname: "canoe"
    creatortoolversion: "13.0.118"
       creatorusername: "michellw"
        creatorcomment: "created using mdflog version 1.5.4.0 and mdf4lib version 1.7.0.5  x64 (2019-10-22)"
   file contents
            attachment: [5x7 table]
     channelgroupcount: 62

标识 can 数据帧

根据总线日志记录的 相关联标准,为 can 总线系统定义的事件类型可以是“can_dataframe”、“can_remoteframe”、“can_errorframe”或“can_overloadframe”。此示例的重点是提取 can 数据帧,因此将使用“can_dataframe”事件类型作为示例来讨论总线日志记录标准。此外,请注意,标准 can 数据帧的有效负载最多包含 8 个字节,用于传输信号值。

该标准规定事件结构体的通道名称应以事件类型名称为前缀,例如“can_dataframe”。通常,使用点作为分隔符来指定成员通道,例如“can_dataframe.id”或“can_dataframe.datalength”。

使用 函数查找与“can_dataframe.*”匹配的通道名称。返回包含匹配通道信息的表。

mdfchannelinfo("logging_mdf.mf4", channel="can_dataframe.*")
ans=24×13 table
                name                 groupnumber    groupnumsamples    groupacquisitionname    groupcomment    groupsourcename    groupsourcepath      displayname         unit                                 comment                              extendednameprefix    sourcename     sourcepath
    _____________________________    ___________    _______________    ____________________    ____________    _______________    _______________    _______________    ___________    __________________________________________________________    __________________    ___________    __________
    "can_dataframe.bitcount"             17              8889                  can1                          can_dataframe     "bitcount"             frame length in bits.                                                can1                  can1   
    "can_dataframe.bitcount"             29              7648                  can2                          can_dataframe     "bitcount"             frame length in bits.                                                can2                  can2   
    "can_dataframe.buschannel"           17              8889                  can1                          can_dataframe     "buschannel"           logical bus channel number the frame was sent or received.           can1                  can1   
    "can_dataframe.buschannel"           29              7648                  can2                          can_dataframe     "buschannel"           logical bus channel number the frame was sent or received.           can2                  can2   
    "can_dataframe.dlc"                  17              8889                  can1                          can_dataframe     "dlc"                  data length code.                                                    can1                  can1   
    "can_dataframe.dlc"                  29              7648                  can2                          can_dataframe     "dlc"                  data length code.                                                    can2                  can2   
    "can_dataframe.databytes"            17              8889                  can1                          can_dataframe     "databytes"            payload data bytes containing the signal values.                     can1                  can1   
    "can_dataframe.databytes"            29              7648                  can2                          can_dataframe     "databytes"            payload data bytes containing the signal values.                     can2                  can2   
    "can_dataframe.datalength"           17              8889                  can1                          can_dataframe     "datalength"           length of stored payload in bytes.                                   can1                  can1   
    "can_dataframe.datalength"           29              7648                  can2                          can_dataframe     "datalength"           length of stored payload in bytes.                                   can2                  can2   
    "can_dataframe.dir"                  17              8889                  can1                          can_dataframe     "dir"                  bit signal indicating the direction (rx, tx).                        can1                  can1   
    "can_dataframe.dir"                  29              7648                  can2                          can_dataframe     "dir"                  bit signal indicating the direction (rx, tx).                        can2                  can2   
    "can_dataframe.flags"                17              8889                  can1                          can_dataframe     "flags"                combination of bit flags for the message.                            can1                  can1   
    "can_dataframe.flags"                29              7648                  can2                          can_dataframe     "flags"                combination of bit flags for the message.                            can2                  can2   
    "can_dataframe.frameduration"        17              8889                  can1                          can_dataframe     "frameduration"    ns             duration for transmission of the frame in nanoseconds.               can1                  can1   
    "can_dataframe.frameduration"        29              7648                  can2                          can_dataframe     "frameduration"    ns             duration for transmission of the frame in nanoseconds.               can2                  can2   
      ⋮

感兴趣的动力总成系统数据是从 can 2 网络记录的。上面的输出显示来自 can 2 网络的数据已存储在 mdf 文件的通道组 29 中。通过指定 groupnumber 选项,使用 函数查看有关通道组 29 的详细信息。

mdfchannelgroupinfo("logging_mdf.mf4", groupnumber=29)
ans=1×13 table
    groupnumber    acquisitionname      comment      numsamples    datasize    sorted    sourcename      sourcepath      sourcecomment    sourcetype    sourcebustype    sourcebuschannelnumber    sourcesimulated
    ___________    _______________    ___________    __________    ________    ______    ___________    _____________    _____________    __________    _____________    ______________________    _______________
        29              can2                 7648        206496     true          can_dataframe             bus             can                   2                    false     

使用 mdfchannelinfo 查看通道组 29 中所有通道的详细信息。

mdfchannelinfo("logging_mdf.mf4", groupnumber=29)
ans=13×13 table
                name                 groupnumber    groupnumsamples    groupacquisitionname    groupcomment    groupsourcename    groupsourcepath      displayname         unit                                 comment                              extendednameprefix    sourcename     sourcepath 
    _____________________________    ___________    _______________    ____________________    ____________    _______________    _______________    _______________    ___________    __________________________________________________________    __________________    ___________    ___________
    "can_dataframe.bitcount"             29              7648                  can2                          can_dataframe     "bitcount"             frame length in bits.                                                can2               can2       
    "can_dataframe.buschannel"           29              7648                  can2                          can_dataframe     "buschannel"           logical bus channel number the frame was sent or received.           can2               can2       
    "can_dataframe.dlc"                  29              7648                  can2                          can_dataframe     "dlc"                  data length code.                                                    can2               can2       
    "can_dataframe.databytes"            29              7648                  can2                          can_dataframe     "databytes"            payload data bytes containing the signal values.                     can2               can2       
    "can_dataframe.datalength"           29              7648                  can2                          can_dataframe     "datalength"           length of stored payload in bytes.                                   can2               can2       
    "can_dataframe.dir"                  29              7648                  can2                          can_dataframe     "dir"                  bit signal indicating the direction (rx, tx).                        can2               can2       
    "can_dataframe.flags"                29              7648                  can2                          can_dataframe     "flags"                combination of bit flags for the message.                            can2               can2       
    "can_dataframe.frameduration"        29              7648                  can2                          can_dataframe     "frameduration"    ns             duration for transmission of the frame in nanoseconds.               can2               can2       
    "can_dataframe.id"                   29              7648                  can2                          can_dataframe     "id"                   id of the can message.                                               can2               can2       
    "can_dataframe.ide"                  29              7648                  can2                          can_dataframe     "ide"                  identifier extension bit.                                            can2               can2       
    "can_dataframe.singlewire"           29              7648                  can2                          can_dataframe     "singlewire"           bit flag indicating a single wire operation.                         can2               can2       
    "can_dataframe.wakeup"               29              7648                  can2                          can_dataframe     "wakeup"               bit flag indicating a wake-up message (high voltage).                can2               can2       
    "t"                                  29              7648                  can2                          can_dataframe     ""                 s                                                                        can2               

从 mdf 文件中读取 can 数据帧

使用 函数将通道组 29 中的所有数据读入一个时间表中。时间表的结构遵循 asam mdf 标准日志记录格式。每行表示来自总线的一个原始 can 帧,而每列表示指定通道组中的一个通道。通道,如“can_dataframe.dir”,是按照总线日志记录标准命名的。

data = mdfread("logging_mdf.mf4", groupnumber=29)
data = 1x1 cell array
    {7648x12 timetable}
candata = data{1}
candata=7648×12 timetable
        t         can_dataframe.buschannel    can_dataframe.flags    can_dataframe.dir    can_dataframe.singlewire    can_dataframe.wakeup    can_dataframe.id    can_dataframe.ide    can_dataframe.frameduration    can_dataframe.bitcount    can_dataframe.dlc    can_dataframe.datalength       can_dataframe.databytes    
    __________    ________________________    ___________________    _________________    ________________________    ____________________    ________________    _________________    ___________________________    ______________________    _________________    ________________________    ______________________________
    2.2601 sec               2                         1                   "tx"                      0                         0                     103                  0                      128000                         67                      2                       2                {[                       1 0]}
    2.2801 sec               2                         1                   "tx"                      0                         0                     103                  0                      128000                         67                      2                       2                {[                       1 0]}
    2.3002 sec               2                         1                   "tx"                      0                         0                     100                  0                      232000                        119                      8                       8                {[      238 2 25 1 0 0 238 2]}
    2.3005 sec               2                         1                   "tx"                      0                         0                     102                  0                      240000                        123                      8                       8                {[       0 128 59 68 0 0 0 0]}
    2.3006 sec               2                         1                   "tx"                      0                         0                     103                  0                      128000                         67                      2                       2                {[                       1 0]}
    2.3008 sec               2                         1                   "tx"                      0                         0                     201                  0                      196000                        101                      6                       6                {[            0 0 0 0 172 38]}
    2.3009 sec               2                         1                   "tx"                      0                         0                    1020                  0                      110000                         58                      1                       1                {[                         1]}
    2.3201 sec               2                         1                   "tx"                      0                         0                     103                  0                      128000                         67                      2                       2                {[                       1 0]}
    2.3401 sec               2                         1                   "tx"                      0                         0                     103                  0                      128000                         67                      2                       2                {[                       1 0]}
    2.3502 sec               2                         1                   "tx"                      0                         0                     100                  0                      234000                        120                      8                       8                {[      4 0 25 2 119 1 238 2]}
    2.3505 sec               2                         1                   "tx"                      0                         0                     102                  0                      228000                        117                      8                       8                {[53 127 119 64 0 128 187 67]}
    2.3507 sec               2                         1                   "tx"                      0                         0                     201                  0                      198000                        102                      6                       6                {[             0 0 0 0 35 40]}
    2.3508 sec               2                         1                   "tx"                      0                         0                    1020                  0                      110000                         58                      1                       1                {[                         1]}
    2.3601 sec               2                         1                   "tx"                      0                         0                     103                  0                      128000                         67                      2                       2                {[                       1 0]}
    2.3801 sec               2                         1                   "tx"                      0                         0                     103                  0                      128000                         67                      2                       2                {[                       1 0]}
    2.4002 sec               2                         1                   "tx"                      0                         0                     100                  0                      234000                        120                      8                       8                {[     10 0 25 3 119 1 238 2]}
      ⋮

使用 dbc 文件解码 can 报文

使用 函数打开 dbc 文件。

candb = candatabase("powertrain_mdf.dbc")
candb = 
  database with properties:
             name: 'powertrain_mdf'
             path: '/tmp/bdoc23a_2181783_2332753/tp3fb84b34/vnt-ex42187575/powertrain_mdf.dbc'
        utf8_file: '/tmp/bdoc23a_2181783_2332753/tp3fb84b34/vnt-ex42187575/powertrain_mdf.dbc'
            nodes: {2x1 cell}
         nodeinfo: [2x1 struct]
         messages: {12x1 cell}
      messageinfo: [12x1 struct]
       attributes: {11x1 cell}
    attributeinfo: [11x1 struct]
         userdata: []

函数使用数据库来解码报文名称和信号。asam 标准日志记录格式数据的时间表转换为 vehicle network toolbox™ can 报文时间表。

msgtimetable = canmessagetimetable(candata, candb)
msgtimetable=7648×8 timetable
       time        id     extended           name                        data                 length      signals       error    remote
    __________    ____    ________    __________________    ______________________________    ______    ____________    _____    ______
    2.2601 sec     103     false      {'ignition_info' }    {[                       1 0]}      2       {1x1 struct}    false    false 
    2.2801 sec     103     false      {'ignition_info' }    {[                       1 0]}      2       {1x1 struct}    false    false 
    2.3002 sec     100     false      {'enginedata'    }    {[      238 2 25 1 0 0 238 2]}      8       {1x1 struct}    false    false 
    2.3005 sec     102     false      {'enginedataieee'}    {[       0 128 59 68 0 0 0 0]}      8       {1x1 struct}    false    false 
    2.3006 sec     103     false      {'ignition_info' }    {[                       1 0]}      2       {1x1 struct}    false    false 
    2.3008 sec     201     false      {'absdata'       }    {[            0 0 0 0 172 38]}      6       {1x1 struct}    false    false 
    2.3009 sec    1020     false      {'gearboxinfo'   }    {[                         1]}      1       {1x1 struct}    false    false 
    2.3201 sec     103     false      {'ignition_info' }    {[                       1 0]}      2       {1x1 struct}    false    false 
    2.3401 sec     103     false      {'ignition_info' }    {[                       1 0]}      2       {1x1 struct}    false    false 
    2.3502 sec     100     false      {'enginedata'    }    {[      4 0 25 2 119 1 238 2]}      8       {1x1 struct}    false    false 
    2.3505 sec     102     false      {'enginedataieee'}    {[53 127 119 64 0 128 187 67]}      8       {1x1 struct}    false    false 
    2.3507 sec     201     false      {'absdata'       }    {[             0 0 0 0 35 40]}      6       {1x1 struct}    false    false 
    2.3508 sec    1020     false      {'gearboxinfo'   }    {[                         1]}      1       {1x1 struct}    false    false 
    2.3601 sec     103     false      {'ignition_info' }    {[                       1 0]}      2       {1x1 struct}    false    false 
    2.3801 sec     103     false      {'ignition_info' }    {[                       1 0]}      2       {1x1 struct}    false    false 
    2.4002 sec     100     false      {'enginedata'    }    {[     10 0 25 3 119 1 238 2]}      8       {1x1 struct}    false    false 
      ⋮

查看“enginedata”报文中存储的信号。

msgtimetable.signals{3}
ans = struct with fields:
    petrollevel: 1
       engpower: 7.5000
       engforce: 0
    idlerunning: 0
        engtemp: 0
       engspeed: 750

重新打包并可视化感兴趣的信号值

使用 函数将来自总线上每个唯一报文的信号数据重新打包为一个信号时间表。此示例根据 can 报文时间表,为三个感兴趣的报文“absdata”、“enginedata”和“gearboxinfo”创建三个单独的信号时间表。

signaltimetable1 = cansignaltimetable(msgtimetable, "absdata")
signaltimetable1=1147×4 timetable
       time       accelerationforce    diagnostics    gearlock    carspeed
    __________    _________________    ___________    ________    ________
    2.3008 sec          -100                0            0            0   
    2.3507 sec           275                0            0            0   
    2.4008 sec           275                0            0            0   
    2.4507 sec           275                0            0            0   
    2.5008 sec           275                0            0            0   
    2.5507 sec           275                0            0            0   
    2.6008 sec           275                0            0            0   
    2.6507 sec           275                0            0            0   
    2.7008 sec           350                0            0            0   
    2.7507 sec           425                0            0          0.5   
    2.8008 sec           425                0            0          0.5   
    2.8507 sec           500                0            0          0.5   
    2.9008 sec           575                0            0          0.5   
    2.9507 sec           575                0            0          0.5   
    3.0008 sec           650                0            0          0.5   
    3.0507 sec           725                0            0          0.5   
      ⋮
signaltimetable2 = cansignaltimetable(msgtimetable, "enginedata")
signaltimetable2=1147×6 timetable
       time       petrollevel    engpower    engforce    idlerunning    engtemp    engspeed
    __________    ___________    ________    ________    ___________    _______    ________
    2.3002 sec         1            7.5          0            0            0         750   
    2.3502 sec         2            7.5        375            0            0           4   
    2.4002 sec         3            7.5        375            0            0          10   
    2.4502 sec         4            7.5        375            0            0          17   
    2.5002 sec         5            7.5        375            0            0          23   
    2.5502 sec         6            7.5        375            0            0          30   
    2.6002 sec         7            7.5        375            0            0          36   
    2.6502 sec         8            7.5        375            0            0          43   
    2.7002 sec         9              9        450            0            0          50   
    2.7502 sec        10           10.5        525            0            0          59   
    2.8002 sec        10           10.5        525            0            0          69   
    2.8502 sec        11             12        600            0            0          80   
    2.9002 sec        11           13.5        675            0            0          92   
    2.9502 sec        12           13.5        675            0            0         106   
    3.0002 sec        13             15        750            0            0         121   
    3.0502 sec        13           16.5        825            0            0         136   
      ⋮
signaltimetable3 = cansignaltimetable(msgtimetable, "gearboxinfo")
signaltimetable3=1147×3 timetable
       time       ecomode    shiftrequest    gear
    __________    _______    ____________    ____
    2.3009 sec       0            0           1  
    2.3508 sec       0            0           1  
    2.4009 sec       0            0           1  
    2.4508 sec       0            0           1  
    2.5009 sec       0            0           1  
    2.5508 sec       0            0           1  
    2.6009 sec       0            0           1  
    2.6508 sec       0            0           1  
    2.7009 sec       0            0           1  
    2.7508 sec       0            0           1  
    2.8009 sec       0            0           1  
    2.8508 sec       0            0           1  
    2.9009 sec       0            0           1  
    2.9508 sec       0            0           1  
    3.0009 sec       0            0           1  
    3.0508 sec       0            0           1  
      ⋮

为了可视化感兴趣的信号,可以绘制信号时间表中的列随时间变化的图,以便进一步分析。

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 {\itenginedata} 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")

figure contains 3 axes objects. axes object 1 with title c a r s p e e d blank s i g n a l blank f r o m blank a b s d a t a message, xlabel timestamp, ylabel car speed contains an object of type line. axes object 2 with title e n g s p e e d blank s i g n a l blank f r o m blank e n g i n e d a t a message, xlabel timestamp, ylabel engine speed contains an object of type line. axes object 3 with title g e a r blank s i g n a l blank f r o m blank g e a r b o x i n f o message, xlabel timestamp, ylabel gear contains an object of type line.

配置通道组和通道元数据

此示例的最终目标是将三条感兴趣的报文“absdata”、“enginedata”和“gearboxinfo”解码后的信号数据写回到同一个 mdf 文件的三个新通道组中。这样,原始数据和解码后的数据可以存储在一个文件中。

除了每个时间表中的数据之外,元数据也是重要元素,它可以提供关于测量环境的描述性信息。本节说明如何配置数据附带的元数据,这是写入文件之前的可选准备步骤。

使用函数 将该特定通道组及其通道的默认或推断元数据添加到每个信号时间表中。

signaltimetable1 = mdfaddchannelgroupmetadata(signaltimetable1);
signaltimetable2 = mdfaddchannelgroupmetadata(signaltimetable2);
signaltimetable3 = mdfaddchannelgroupmetadata(signaltimetable3);

查看每个通道组及其通道的默认元数据。

signaltimetable1.properties.customproperties
ans = 
customproperties with properties:
           channelgroupacquisitionname: ""
                   channelgroupcomment: ""
                channelgroupsourcename: ""
                channelgroupsourcepath: ""
             channelgroupsourcecomment: ""
                channelgroupsourcetype: unspecified
             channelgroupsourcebustype: unspecified
    channelgroupsourcebuschannelnumber: 0
                    channeldisplayname: [""    ""    ""    ""]
                        channelcomment: [""    ""    ""    ""]
                           channelunit: [""    ""    ""    ""]
                           channeltype: [fixedlength    fixedlength    fixedlength    fixedlength]
                       channeldatatype: [reallittleendian    reallittleendian    reallittleendian    reallittleendian]
                        channelnumbits: [64 64 64 64]
                  channelcomponenttype: [none    none    none    none]
                channelcompositiontype: [none    none    none    none]
                     channelsourcename: [""    ""    ""    ""]
                     channelsourcepath: [""    ""    ""    ""]
                  channelsourcecomment: [""    ""    ""    ""]
                     channelsourcetype: [unspecified    unspecified    unspecified    unspecified]
                  channelsourcebustype: [unspecified    unspecified    unspecified    unspecified]
         channelsourcebuschannelnumber: [0 0 0 0]
                     channelreadoption: [missing    missing    missing    missing]
signaltimetable2.properties.customproperties
ans = 
customproperties with properties:
           channelgroupacquisitionname: ""
                   channelgroupcomment: ""
                channelgroupsourcename: ""
                channelgroupsourcepath: ""
             channelgroupsourcecomment: ""
                channelgroupsourcetype: unspecified
             channelgroupsourcebustype: unspecified
    channelgroupsourcebuschannelnumber: 0
                    channeldisplayname: [""    ""    ""    ""    ""    ""]
                        channelcomment: [""    ""    ""    ""    ""    ""]
                           channelunit: [""    ""    ""    ""    ""    ""]
                           channeltype: [fixedlength    fixedlength    fixedlength    fixedlength    fixedlength    fixedlength]
                       channeldatatype: [reallittleendian    reallittleendian    reallittleendian    reallittleendian    reallittleendian    reallittleendian]
                        channelnumbits: [64 64 64 64 64 64]
                  channelcomponenttype: [none    none    none    none    none    none]
                channelcompositiontype: [none    none    none    none    none    none]
                     channelsourcename: [""    ""    ""    ""    ""    ""]
                     channelsourcepath: [""    ""    ""    ""    ""    ""]
                  channelsourcecomment: [""    ""    ""    ""    ""    ""]
                     channelsourcetype: [unspecified    unspecified    unspecified    unspecified    unspecified    unspecified]
                  channelsourcebustype: [unspecified    unspecified    unspecified    unspecified    unspecified    unspecified]
         channelsourcebuschannelnumber: [0 0 0 0 0 0]
                     channelreadoption: [missing    missing    missing    missing    missing    missing]
signaltimetable3.properties.customproperties
ans = 
customproperties with properties:
           channelgroupacquisitionname: ""
                   channelgroupcomment: ""
                channelgroupsourcename: ""
                channelgroupsourcepath: ""
             channelgroupsourcecomment: ""
                channelgroupsourcetype: unspecified
             channelgroupsourcebustype: unspecified
    channelgroupsourcebuschannelnumber: 0
                    channeldisplayname: [""    ""    ""]
                        channelcomment: [""    ""    ""]
                           channelunit: [""    ""    ""]
                           channeltype: [fixedlength    fixedlength    fixedlength]
                       channeldatatype: [reallittleendian    reallittleendian    reallittleendian]
                        channelnumbits: [64 64 64]
                  channelcomponenttype: [none    none    none]
                channelcompositiontype: [none    none    none]
                     channelsourcename: [""    ""    ""]
                     channelsourcepath: [""    ""    ""]
                  channelsourcecomment: [""    ""    ""]
                     channelsourcetype: [unspecified    unspecified    unspecified]
                  channelsourcebustype: [unspecified    unspecified    unspecified]
         channelsourcebuschannelnumber: [0 0 0]
                     channelreadoption: [missing    missing    missing]

创建一个数组来包含三个报文名称,创建一个元胞数组来包含三个信号时间表。

msgnames = ["absdata", "enginedata", "gearboxinfo"];
tts = {signaltimetable1, signaltimetable2, signaltimetable3};

tts 中的每个时间表设置属性 channelgroupcomment,以指示从其解码信号数据的 can 报文的名称。

for ii = 1:numel(tts)
    tts{ii}.properties.customproperties.channelgroupcomment = sprintf("signal data from can message ""%s"".", msgnames(ii));
end

对于每个 can 报文,使用此脚本末尾提供的辅助函数 getsignalunitcandb 获取每个信号的单位。将每个时间表变量的属性 channelunit 设置为获得的单位。

for ii = 1:numel(msgnames)
    signames = tts{ii}.properties.variablenames;
    for jj = 1:numel(signames)
        sigunit = getsignalunit(candb, msgnames(ii), signames(jj));
        tts{ii}.properties.customproperties.channelunit(jj) = sigunit;
    end
end

查看每个通道组及其通道的更新后的元数据。请注意属性 channelgroupcommentchannelunit 中更新后的值。

for ii = 1:numel(tts)
    tts{ii}.properties.customproperties
end
ans = 
customproperties with properties:
           channelgroupacquisitionname: ""
                   channelgroupcomment: "signal data from can message "absdata"."
                channelgroupsourcename: ""
                channelgroupsourcepath: ""
             channelgroupsourcecomment: ""
                channelgroupsourcetype: unspecified
             channelgroupsourcebustype: unspecified
    channelgroupsourcebuschannelnumber: 0
                    channeldisplayname: [""    ""    ""    ""]
                        channelcomment: [""    ""    ""    ""]
                           channelunit: ["n"    ""    ""    "mph"]
                           channeltype: [fixedlength    fixedlength    fixedlength    fixedlength]
                       channeldatatype: [reallittleendian    reallittleendian    reallittleendian    reallittleendian]
                        channelnumbits: [64 64 64 64]
                  channelcomponenttype: [none    none    none    none]
                channelcompositiontype: [none    none    none    none]
                     channelsourcename: [""    ""    ""    ""]
                     channelsourcepath: [""    ""    ""    ""]
                  channelsourcecomment: [""    ""    ""    ""]
                     channelsourcetype: [unspecified    unspecified    unspecified    unspecified]
                  channelsourcebustype: [unspecified    unspecified    unspecified    unspecified]
         channelsourcebuschannelnumber: [0 0 0 0]
                     channelreadoption: [missing    missing    missing    missing]
ans = 
customproperties with properties:
           channelgroupacquisitionname: ""
                   channelgroupcomment: "signal data from can message "enginedata"."
                channelgroupsourcename: ""
                channelgroupsourcepath: ""
             channelgroupsourcecomment: ""
                channelgroupsourcetype: unspecified
             channelgroupsourcebustype: unspecified
    channelgroupsourcebuschannelnumber: 0
                    channeldisplayname: [""    ""    ""    ""    ""    ""]
                        channelcomment: [""    ""    ""    ""    ""    ""]
                           channelunit: ["l"    "kw"    "n"    ""    "degc"    "rpm"]
                           channeltype: [fixedlength    fixedlength    fixedlength    fixedlength    fixedlength    fixedlength]
                       channeldatatype: [reallittleendian    reallittleendian    reallittleendian    reallittleendian    reallittleendian    reallittleendian]
                        channelnumbits: [64 64 64 64 64 64]
                  channelcomponenttype: [none    none    none    none    none    none]
                channelcompositiontype: [none    none    none    none    none    none]
                     channelsourcename: [""    ""    ""    ""    ""    ""]
                     channelsourcepath: [""    ""    ""    ""    ""    ""]
                  channelsourcecomment: [""    ""    ""    ""    ""    ""]
                     channelsourcetype: [unspecified    unspecified    unspecified    unspecified    unspecified    unspecified]
                  channelsourcebustype: [unspecified    unspecified    unspecified    unspecified    unspecified    unspecified]
         channelsourcebuschannelnumber: [0 0 0 0 0 0]
                     channelreadoption: [missing    missing    missing    missing    missing    missing]
ans = 
customproperties with properties:
           channelgroupacquisitionname: ""
                   channelgroupcomment: "signal data from can message "gearboxinfo"."
                channelgroupsourcename: ""
                channelgroupsourcepath: ""
             channelgroupsourcecomment: ""
                channelgroupsourcetype: unspecified
             channelgroupsourcebustype: unspecified
    channelgroupsourcebuschannelnumber: 0
                    channeldisplayname: [""    ""    ""]
                        channelcomment: [""    ""    ""]
                           channelunit: [""    ""    ""]
                           channeltype: [fixedlength    fixedlength    fixedlength]
                       channeldatatype: [reallittleendian    reallittleendian    reallittleendian]
                        channelnumbits: [64 64 64]
                  channelcomponenttype: [none    none    none]
                channelcompositiontype: [none    none    none]
                     channelsourcename: [""    ""    ""]
                     channelsourcepath: [""    ""    ""]
                  channelsourcecomment: [""    ""    ""]
                     channelsourcetype: [unspecified    unspecified    unspecified]
                  channelsourcebustype: [unspecified    unspecified    unspecified]
         channelsourcebuschannelnumber: [0 0 0]
                     channelreadoption: [missing    missing    missing]

使用可自定义的通道组和通道元数据将数据写入 mdf 文件

使用 函数,将 tts 中的三个时间表逐个写入 logging_mdf.mf4。由于 mdf 文件当前有 62 个通道组,因此 tts{1}tts{2}tts{3} 将作为通道组 63、64 和 65 写入文件的末尾。

for ii = 1:numel(tts)
    mdfwrite("logging_mdf.mf4", tts{ii})
end

将 dbc 文件作为附件添加到 mdf 文件中

为了方便地访问 powertrain_mdf.dbc 以便将来执行额外的数据解码,您可以使用 将 dbc 文件作为嵌入附件添加到 mdf 文件中。

mdfaddattachment("logging_mdf.mf4", "powertrain_mdf.dbc", embedded=true)

关闭 dbc 文件

通过从工作区中清除 dbc 文件的变量,关闭对 dbc 文件的访问。

clear candb

辅助函数

function sigunit = getsignalunit(candb, msgname, signame)
% look up the unit of a signal in a message from a can database. return the unit as a string.
    msgidx = find(strcmp({candb.messageinfo.name}, msgname));
    sigidx = find(strcmp({candb.messageinfo(msgidx).signalinfo.name}, signame));
    sigunit = string(candb.messageinfo(msgidx).signalinfo(sigidx).units);
end
网站地图