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 property of the returned object. The MDF file currently has one embedded attachment named ReadMe.txt.

info = mdfInfo("VehicleData.mf4");
info.Attachment
ans=1×7 table
        Name        Comment      Type          MIMEType         Size    EmbeddedSize               MD5CheckSum            
    ____________    _______    ________    _________________    ____    ____________    __________________________________

    "ReadMe.txt"      ""       Embedded    "application/txt"     7           7          "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×7 table
          Name          Comment      Type          MIMEType         Size    EmbeddedSize               MD5CheckSum            
    ________________    _______    ________    _________________    ____    ____________    __________________________________

    "ReadMe.txt"          ""       Embedded    "application/txt"      7           7         "9166BA27E54335BA1043A5FF023E8F4E"
    "HelloWorld.txt"      ""       Embedded    "application/txt"     14          14         "BEA8252FF4E80F41719EA13CDF007273"

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×7 table
               Name                             Comment                   Type          MIMEType         Size    EmbeddedSize               MD5CheckSum            
    ___________________________    _________________________________    ________    _________________    ____    ____________    __________________________________

    "ReadMe.txt"                   ""                                   Embedded    "application/txt"      7           7         "9166BA27E54335BA1043A5FF023E8F4E"
    "HelloWorld.txt"               ""                                   Embedded    "application/txt"     14          14         "BEA8252FF4E80F41719EA13CDF007273"
    "myFolder/HelloWorld10.txt"    "Repeat hello world for 10 times"    External    "application/txt"    161           0         "99B4FDE65B883C034D1997A6DFD27700"

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×7 table
               Name                             Comment                   Type          MIMEType         Size    EmbeddedSize               MD5CheckSum            
    ___________________________    _________________________________    ________    _________________    ____    ____________    __________________________________

    "ReadMe.txt"                   ""                                   Embedded    "application/txt"      7          7          "9166BA27E54335BA1043A5FF023E8F4E"
    "myFolder/HelloWorld10.txt"    "Repeat hello world for 10 times"    External    "application/txt"    161          0          "99B4FDE65B883C034D1997A6DFD27700"

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.Name(2)
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=1×7 table
        Name        Comment      Type          MIMEType         Size    EmbeddedSize               MD5CheckSum            
    ____________    _______    ________    _________________    ____    ____________    __________________________________

    "ReadMe.txt"      ""       Embedded    "application/txt"     7           7          "9166BA27E54335BA1043A5FF023E8F4E"

Save an Embedded Attachment from MDF File

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

mdfSaveAttachment("VehicleData.mf4", Attachment="ReadMe.txt")