主要内容

Write Compressed Data to MDF Files

This example shows how to write data to MDF files using compression, enhancing storage efficiency.

The MAT file MDFTimetables.mat used in this example contains two timetables.

  • Timetable TT1 has variables of numeric data types, including uint8, uint16, uint32, uint64, int8, int16, int32, int64, single, and double.

  • Timetable TT2 has variables of string and byte array data types, including both fixed-length and variable-length data.

Introduction

According to the ASAM MDF standard, the Data Zipped Block (DZBLOCK), introduced in MDF version 4.1, allows for the compression of various data blocks within MDF files, which is particularly useful when disk space is limited. By compressing signal data during the writing process, it effectively reduces file size. However, for very small data sections, the compression overhead might not provide a size advantage. Tools for reading can decompress the data to access the original data.

ASAM MDF V4.2.0 supports the following DZBLOCK compression algorithms:

  • dz_zip_type = 0: Deflate

  • dz_zip_type = 1: Transposition + Deflate

The mdfWrite function by default does not write with compression. To write with compression, the CompressionType name-value argument must be set to a value other than "none".

The CompressionType name-value argument for mdfWrite controls the compression algorithm used when writing data to the MDF file:

  • "none" (default): Writes uncompressed data.

  • "deflate": Writes compressed data using the Deflate algorithm.

  • "transposeDeflate": Writes compressed data using the Transpose-Deflate algorithm.

The CompressionLevel name-value argument accepts a numerical value and controls the level of compression, depending on the CompressionType:

  • For "none": the default and only acceptable value is 0.

  • For "deflate" and "transposeDeflate": the default value is 6 and acceptable values are from 0 (no compression) to 9 (maximum compression). Increasing the compression level will result in longer writing times.

Load Timetables into Workspace

Load the timetable variables, TT1 and TT2, from MDFTimetables.mat into the workspace.

load("MDFTimetables.mat")

Write Compressed Data Directly to a New MDF File

Use the mdfWrite function with the target MDF file name, the timetable variable TT1, and the "deflate" compression type specified as input arguments. Since the CompressionLevel name-value argument is not specified, the default compression level of the specified compression type will be used, in this case the default level for "deflate" is 6. Data in TT1 is written to the first available channel group index of the file, which is channel group 1 since CompressedSignalData.mf4 is a newly created file.

mdfWrite("CompressedSignalData.mf4", TT1, CompressionType="deflate")
Warning: Specified target file did not exist. Created file: 
C:\ExampleManager9a362a\user.Bdoc.Compression_Example\vnt-ex52002539\CompressedSignalData.mf4

Writing data with compression is channel group based, meaning that a different compression type can be used for writing each channel group. Call mdfWrite again to write TT2 to the next available channel group index 2, this time writing with a compression type of "transposeDeflate" and a compression level of 9 (maximum compression, slowest speed).

mdfWrite("CompressedSignalData.mf4", TT2, CompressionType="transposeDeflate", CompressionLevel=9)

Examine the Data

To verify that data was successfully written to file, read data from both channel groups using mdfRead and examine the results.

data = mdfRead("CompressedSignalData.mf4")
data=2×1 cell array
    100×10 timetable
      10×4 timetable

chanGrp1Data = data{1}
chanGrp1Data=100×10 timetable
      0 sec     0    200    600    1400    -99    -198    -396    -794    -9.8000    -19.6000
    0.1 sec     2    204    608    1416    -97    -194    -388    -778    -9.6000    -19.2000
    0.2 sec     4    208    616    1432    -95    -190    -380    -762    -9.4000    -18.8000
    0.3 sec     6    212    624    1448    -93    -186    -372    -746    -9.2000    -18.4000
    0.4 sec     8    216    632    1464    -91    -182    -364    -730         -9         -18
    0.5 sec    10    220    640    1480    -89    -178    -356    -714    -8.8000    -17.6000
    0.6 sec    12    224    648    1496    -87    -174    -348    -698    -8.6000    -17.2000
    0.7 sec    14    228    656    1512    -85    -170    -340    -682    -8.4000    -16.8000
    0.8 sec    16    232    664    1528    -83    -166    -332    -666    -8.2000    -16.4000
    0.9 sec    18    236    672    1544    -81    -162    -324    -650         -8         -16
      1 sec    20    240    680    1560    -79    -158    -316    -634    -7.8000    -15.6000
    1.1 sec    22    244    688    1576    -77    -154    -308    -618    -7.6000    -15.2000
    1.2 sec    24    248    696    1592    -75    -150    -300    -602    -7.4000    -14.8000
    1.3 sec    26    252    704    1608    -73    -146    -292    -586    -7.2000    -14.4000
      ⋮

chanGrp2Data = data{2}
chanGrp2Data=10×4 timetable
    0 sec    "abcd"    [255,255,255,255,255]     "zero"                [255,255]
    1 sec    "efgh"         [18,35,52,69,86]      "one"         [18,35,52,69,86]
    2 sec    "ijkl"              [0,1,2,3,4]      "two"        [0,1,2,3,4,5,6,7]
    3 sec    "mnop"              [4,3,2,1,0]    "three"              [4,3,2,1,0]
    4 sec    "qrst"    [255,254,253,252,251]     "four"            [253,252,251]
    5 sec    "uvwx"    [250,249,248,247,246]     "five"    [250,249,248,247,246]
    6 sec    "yzAB"    [245,244,243,242,241]      "six"    [245,244,243,242,241]
    7 sec    "CDEF"    [240,239,238,237,236]    "seven"            [240,238,236]
    8 sec    "GHIJ"    [235,234,233,232,231]    "eight"            [235,233,231]
    9 sec    "KLMN"    [255,255,255,255,255]     "nine"    [255,255,255,255,255]

Compare File Size to MDF File with Uncompressed Signal Data

Use the mdfWrite function to write TT1 and TT2 without compression to a new MDF file called UncompressedSignalData.mf4.

mdfWrite("UncompressedSignalData.mf4", TT1)
Warning: Specified target file did not exist. Created file: 
C:\ExampleManager9a362a\user.Bdoc.Compression_Example\vnt-ex52002539\UncompressedSignalData.mf4
mdfWrite("UncompressedSignalData.mf4", TT2)

Use the dir function to get the file size in bytes of both MDF files and compare.

compressedFileSizeBytes = dir("CompressedSignalData.mf4").bytes
compressedFileSizeBytes = 
11864
uncompressedFileSizeBytes = dir("UncompressedSignalData.mf4").bytes
uncompressedFileSizeBytes = 
14640
reductionPercentage = ((uncompressedFileSizeBytes - compressedFileSizeBytes) / uncompressedFileSizeBytes) * 100;
fprintf("%.2f%% reduction in MDF file size when writing signal data with compression.\n", reductionPercentage)
18.96% reduction in MDF file size when writing signal data with compression.

See Also

Functions