Main Content

Import and Export MATLAB Objects Using MongoDB and MongoDB C++ Interface

This example shows how to export objects from the MATLAB® workspace into MongoDB® using the MongoDB C++ interface. The export serializes the objects in MongoDB. Then, the example shows how to import objects back into the MATLAB workspace. The import deserializes the objects and recreates them in MATLAB for method execution. After the export and import, the example shows how to drop the collection.

In this example, the objects belong to the TensileData class. This class is a sample class in MATLAB. The data used to create the objects is sample data. For details, see Representing Structured Data with Classes. To run the code in this example, you define the class in the current folder.

The sample data represents tensile stress and strain measurements that you can use to calculate the elastic modulus of various materials. In simple terms, stress is the force applied to a material, and strain is the resulting deformation. The ratio of stress to strain defines a characteristic of the material.

Create Objects

Create the TensileData objects tdcs for carbon steel materials and tdss for stainless steel materials.

tdcs = TensileData('carbon steel',1, ...
    [2e4 4e4 6e4 8e4],[.12 .20 .31 .40]);
tdss = TensileData('stainless steel',1, ...
    [2e4 4e4 6e4 8e4],[.06 .10 .16 .20]);

Connect to MongoDB C++ Interface

Create a MongoDB connection to the database mongotest using the MongoDB C++ interface. Here, the database server dbtb01 hosts this database using port number 27017.

server = "dbtb01";
port = 27017;
dbname = "mongotest";
conn = mongoc(server,port,dbname)
conn = 

  connection with properties:

               Database: "mongotest"
               UserName: ""
                 Server: "dbtb01"
                   Port: 27017
        CollectionNames: [13×1 string]

conn is the mongo object that contains the MongoDB connection. The object properties contain information about the connection and the database.

  • The database name is mongotest.

  • The user name is blank.

  • The database server is dbtb01.

  • The port number is 27017.

  • This database contains 13 document collections.

Verify the MongoDB connection.

isopen(conn)
ans =

  logical

   1

The database connection is successful because the isopen function returns 1. Otherwise, the database connection is closed.

Create Collection in MongoDB

Create the TensileData collection using the MongoDB connection.

collection = "TensileData";
createCollection(conn,collection)

Export Objects into MongoDB

Export the TensileData objects into the collection. The insert function serializes the TensileData objects into a JSON-style structure. ntdcs and ntdss contain the number of objects exported into the collection.

ntdcs = insert(conn,collection,tdcs);
ntdss = insert(conn,collection,tdss);

Import Objects into MATLAB Workspace

Import the TensileData objects into the MATLAB workspace. The find function deserializes the TensileData objects into the documents structure array.

documents = find(conn,collection);

Recreate the objects in the MATLAB workspace.

tdcs = TensileData(documents(1).Material,documents(1).SampleNumber, ...
    documents(1).Stress,documents(1).Strain);
tdss = TensileData(documents(2).Material,documents(2).SampleNumber, ...
    documents(2).Stress,documents(2).Strain);

You can execute methods of the objects after they appear in the MATLAB workspace. For example, calculate the elastic modulus.

Remove Documents and Drop Collection

Remove all documents from the collection. n contains the number of documents removed from the collection.

n = remove(conn,collection,"{}")
n =

     2

Drop the collection.

dropCollection(conn,collection)

Close MongoDB C++ Interface Connection

close(conn)

See Also

| | | | | | |

External Websites