Database Toolbox Previous page   Next Page

Exporting Multiple New Records from MATLAB

In this example, you import multiple records, manipulate the data in MATLAB, and then export it to a different table in the database. Specifically, you import sales figures for all products, by month, into MATLAB. Then you compute the total sales for each month. Finally, you export the monthly totals to a new table.

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 a similar set of functions, use the M-file matlab\toolbox\database\dbdemos\dbinsert2demo.m:

  1. If you did not already do so, set up the data source dbtoolboxdemo according to the directions in Setting Up a Data Source. This data source uses the tutorial database.
  2. Check the properties of the tutorial database to be sure it is writable, that is, not read only.
  3. Connect to the database by typing
  1. You define the returned connection object as conn. You do not need a username or password to access the dbtoolboxdemo database.

  1. Specify preferences for the retrieved data by using the setdbprefs function. Set the data return format to numeric and specify that any NULL value read from the database is to be converted to a 0 in MATLAB.
  1. Note that when you specify DataReturnFormat as numeric, the value for NullNumberRead must also be numeric, such as 0. For example, it cannot be a string, such as NaN.

  1. Import the sales figures. Specifically, import all data from the salesVolume table. Type
  2. To get a sense of the data you imported, view the column names in the fetched data set. Type
  1. MATLAB returns

  1. To get a sense of what the data is, view the data for January, which is in column 2. Type
  1. MATLAB returns

  1. Get the size of the matrix containing the fetched data set, assigning the dimensions to m and n. In a later step, you use these values to compute the monthly totals. Type
  1. MATLAB returns

  1. Compute the monthly totals by typing
  1. where tmp is the sales volume for all products in a given month i, and monthly is the total sales volume of all products for the month i.

    For example, when i is 2, row 1 of monthly is the total of all rows in column 2 of curs.Data, where column 2 is the sales volume for January.

    To see the result, type

    MATLAB returns

  1. Create a string array containing the column names into which you are inserting the data. In a later step, we insert the data into the salesTotal column of the yearlySales table. The yearlySales table contains no data. Here we assign the array to the variable colnames. Type
  2. Insert the data into the yearlySales table by typing
  3. View the yearlySales table in the tutorial database to be sure the data was imported correctly.

  4. Close the cursor and database connection. Type

Previous page  Replacing Existing Data in a Database from MATLAB Retrieving BINARY or OTHER Java SQL Data Types Next page

© 1994-2005 The MathWorks, Inc.