Main Content

Work with MDF Attachment Files

This example shows how to add attachment files to an MDF-file, remove attachment files from an MDF-file, and save attachment files embedded in an MDF-file to disk. The MDF-file used in this example VehicleData.mf4 currently has one embedded attachment named ReadMe.txt.

An MDF-file with version 4.0 or higher can have a number of attachments. An attachment can be either physically embedded in the MDF-file or externally referenced by file path and name. The difference is that an embedded attachment adds to the size of the MDF-file, while an external attachment does not. This example uses TXT-files for demonstration, but you can also attach files of other types to an MDF-file.

Examine File Metadata Related to Attachments

To examine the file metadata related to attachments, call mdfInfo with the MDF-file name specified and query the Attachment field of the returned structure. The MDF-file currently has one embedded attachment named ReadMe.txt.

info = mdfInfo("VehicleData.mf4");
info.Attachment
ans = struct with fields:
            Name: 'ReadMe.txt'
            Path: '/tmp/Bdoc22a_1891349_157288/tpe433a46b/vnt-ex75572881/ReadMe.txt'
         Comment: ''
            Type: Embedded
        MIMEType: 'application/txt'
            Size: 7
    EmbeddedSize: 7
     MD5CheckSum: '9166BA27E54335BA1043A5FF023E8F4E'

Add an Embedded Attachment to MDF-File

Create a text file named HelloWorld.txt in the current working directory.

fileID = fopen("HelloWorld.txt", "w");
fprintf(fileID, "Hello, World!\n");
fclose(fileID);

View content of the file using the type function.

type("HelloWorld.txt")
Hello, World!

You will add HelloWorld.txt as an attachment to the MDF-file VehicleData.mf4, which requires permission to modify the MDF-file. First, check if you have write access to the MDF-file. If not, make the MDF-file writable.

[~, values] = fileattrib("VehicleData.mf4");
if ~values.UserWrite
    fileattrib("VehicleData.mf4", "+w")
end

Use function mdfAddAttachment with optional argument Embedded set to true. This option determines whether the attachment is physically embedded in the MDF-file. The default value is false.

mdfAddAttachment("VehicleData.mf4", "HelloWorld.txt", Embedded=true)

To verify that the embedded attachment has been added successfully, examine the file metadata related to attachments. Note that for the second attachment, Size is equal to EmbeddedSize because it is an embedded attachment.

info = mdfInfo("VehicleData.mf4");
info.Attachment
ans=2×1 struct array with fields:
    Name
    Path
    Comment
    Type
    MIMEType
    Size
    EmbeddedSize
    MD5CheckSum

Add an External Attachment to MDF-File

Create a sub-directory named myFolder, and create a text file named HelloWorld10.txt inside this sub-directory.

mkdir myFolder
fileID = fopen(fullfile("myFolder", "HelloWorld10.txt"), "w");
for ii = 1:10
    fprintf(fileID, "Hello, World! %d\n", ii);
end
fclose(fileID);

View content of the file using the type function.

type(fullfile("myFolder", "HelloWorld10.txt"))
Hello, World! 1
Hello, World! 2
Hello, World! 3
Hello, World! 4
Hello, World! 5
Hello, World! 6
Hello, World! 7
Hello, World! 8
Hello, World! 9
Hello, World! 10

Add HelloWorld10.txt as an attachment to the MDF-file VehicleData.mf4 using function mdfAddAttachment with a relative path to the TXT-file. Optional argument Embedded is not specified, which adds the TXT-file as an externally linked attachment. Specify optional argument Comment to add information about the attached file.

mdfAddAttachment("VehicleData.mf4", fullfile("myFolder", "HelloWorld10.txt"), Comment="Repeat hello world for 10 times")

To verify that the external attachment has been added successfully, examine the file metadata related to attachments. Note that for the third attachment, Size is non-zero but EmbeddedSize is zero because it is an external attachment.

info = mdfInfo("VehicleData.mf4");
info.Attachment
ans=3×1 struct array with fields:
    Name
    Path
    Comment
    Type
    MIMEType
    Size
    EmbeddedSize
    MD5CheckSum

Remove an Attachment from MDF-File

Use the mdfRemoveAttachment function to remove the embedded attachment named HelloWorld.txt that has just been added.

mdfRemoveAttachment("VehicleData.mf4", "HelloWorld.txt")

To verify that the embedded attachment has been removed successfully, examine the file metadata related to attachments.

info = mdfInfo("VehicleData.mf4");
info.Attachment
ans=2×1 struct array with fields:
    Name
    Path
    Comment
    Type
    MIMEType
    Size
    EmbeddedSize
    MD5CheckSum

Similarly, remove the external attachment named HelloWorld10.txt that has just been added. To precisely identify the attachment to remove, the specified attachment file name must exactly match the attachment name as seen in info.Attachment.

attachmentName = info.Attachment(2).Name
attachmentName = 
'myFolder/HelloWorld10.txt'
mdfRemoveAttachment("VehicleData.mf4", attachmentName)

To verify that the embedded attachment has been removed successfully, examine the file metadata related to attachments.

info = mdfInfo("VehicleData.mf4");
info.Attachment
ans = struct with fields:
            Name: 'ReadMe.txt'
            Path: '/tmp/Bdoc22a_1891349_157288/tpe433a46b/vnt-ex75572881/ReadMe.txt'
         Comment: ''
            Type: Embedded
        MIMEType: 'application/txt'
            Size: 7
    EmbeddedSize: 7
     MD5CheckSum: '9166BA27E54335BA1043A5FF023E8F4E'

Save Embedded Attachment from MDF-File

Open access to the MDF-file using the mdf function.

mdfObj = mdf("VehicleData.mf4")
mdfObj = 
  MDF with properties:

   File Details
                 Name: 'VehicleData.mf4'
                 Path: '/tmp/Bdoc22a_1891349_157288/tpe433a46b/vnt-ex75572881/VehicleData.mf4'
               Author: ''
           Department: ''
              Project: ''
              Subject: ''
              Comment: 'Example file demonstrating workflows of writing to MDF files.'
              Version: '4.10'
             DataSize: 2252350
     InitialTimestamp: 2022-01-20 01:22:34.000000000

   Creator Details
    ProgramIdentifier: 'MATLAB'
              Creator: [1x1 struct]

   File Contents
           Attachment: [1x1 struct]
         ChannelNames: {2x1 cell}
         ChannelGroup: [1x2 struct]

   Options
           Conversion: Numeric

Using function saveAttachment, save the one remaining embedded attachment named ReadMe.txt to the current MATLAB working folder.

saveAttachment(mdfObj, "ReadMe.txt")

Close MDF-File

Close access to the MDF-file by clearing its variable from the workspace.

clear mdfObj