Database Toolbox Previous page   Next Page

Importing Data into MATLAB from a Database

In this example, you connect to and import data from a database. Specifically, you connect to the SampleDB data source, and then import country data from the customers table in the Nwind sample database.

In this section, you learn to use these Database Toolbox functions:

If you want to see or copy the functions for this example, or if you want to run the set of functions, use the M-file matlab\toolbox\database\dbdemos\dbimportdemo.m:

  1. If you did not already do so, set up the data source SampleDB according to the directions in Setting Up a Data Source.
  2. In MATLAB, set the maximum time, in seconds, you want to allow the MATLAB session to try to connect to a database. This prevents the MATLAB session from hanging up if a database connection fails.
  1. Enter the function before you connect to a database.

    Type

    to specify the maximum allowable connection time as 5 seconds. If you are using a JDBC connection, the function syntax is different. For more information, see logintimeout.

    MATLAB returns

    When you use the database function in the next step to connect to the database, MATLAB tries to make the connection. If it cannot connect in 5 seconds, it stops trying.

  1. Connect to the database by typing
  1. In this example, you define a MATLAB variable, conn, to be the returned connection object. This connection stays open until you close it with the close function.

    For the database function, you provide the name of the database, which is the data source SampleDB for this example. The other two arguments for the database function are username and password. For this example, they are empty strings because the SampleDB database does not require a username or password.

    If you are using a JDBC connection, the database function syntax is different. For more information, see the database reference page.

    For a valid connection, MATLAB returns information about the connection object.

  1. Check the connection status by typing
  1. MATLAB returns status information about the connection, indicating that the connection was successful.

  1. Open a cursor and execute an SQL statement by typing
  1. In the exec function, conn is the name of the connection object. The second argument, select country from customers, is a valid SQL statement that selects the country column of data from the customers table.

    The exec function returns a cursor object. In this example, you assign the returned cursor object to the MATLAB variable curs.

    The data in the cursor object is stored in MATLAB.

    If MATLAB displays an error, the query syntax might be invalid. See Data Retrieval Restrictions for more information.

  1. Specify the format of retrieved data by typing
  1. In this example, the returned data contains strings so the data format must support strings, which cellarray does. If the returned data contains only numerics or if the nonnumeric data is not relevant, you could instead specify the numeric format, which uses less memory.

  1. Import data into MATLAB by typing
  1. fetch is the function that imports data. It has the following two arguments in this example:

    In this example, fetch reassigns the cursor object containing the rows of data returned by fetch to the variable curs. MATLAB returns information about the cursor object.

    The curs object contains an element, Data, that in turn contains the rows of data in the cell array. You can tell that Data contains 10 rows and 1 column.

  1. Display the Data element in the cursor object, curs. Assign the data element, curs.Data to the variable AA. Type
  1. MATLAB returns

    Now you can use MATLAB to perform operations on the returned data. For more information, see Working with Cell Arrays in MATLAB. For more about working with strings, see Characters and Strings in the MATLAB Programming documentation.

    If the returned data includes Java BINARY or OTHER data types, you might have to process the data before using it. See Retrieving BINARY or OTHER Java SQL Data Types for instructions to use this type of data.

  1. At this point, you can go to the next example. If you want to stop working now and resume with the next example at a later time, close the cursor and the connection. Type

Previous page  Using Functions in the Database Toolbox Viewing Information About the Imported Data Next page

© 1994-2005 The MathWorks, Inc.