Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Import and Export MATLAB Objects Using MongoDB

This example shows how to export objects from the MATLAB® workspace into MongoDB® using the Database Toolbox™ interface for MongoDB. 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.

To run this example, you must first install the Database Toolbox interface for MongoDB. For details, see Database Toolbox Interface for MongoDB Installation.

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 Class to Represent Structured Data (MATLAB). To run the code in this example, define the class in the current folder.

The data represents tensile stress or strain measurements. To calculate the elastic modulus of various materials, use this data. 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

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

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

  mongo with properties:

               Database: 'mongotest'
               UserName: ''
                 Server: {'dbtb01'}
                   Port: 27017
        CollectionNames: {'airlinesmall', 'employee', 'largedata' ... and 3 more}
         TotalDocuments: 23485919

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 six document collections. The first three collection names are airlinesmall, employee, and largedata.

  • This database contains 23,485,919 documents.

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.

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 Connection

close(conn)

See Also

| | | | | | |

Related Topics

External Websites

Was this topic helpful?