Database Toolbox |
Exporting Data from MATLAB to a New Record in a Database
In this example, you retrieve a set of data, perform a simple calculation on the data using MATLAB, and export the results as a new record to another table in the database. Specifically, you retrieve freight costs from an orders table, calculate the average freight cost, and put the data into a cell array to export it. Then export the data (the average freight cost and the date the calculation was made) to an empty table.
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\dbinsertdemo.m
.
Note You can use the Visual Query Builder GUI instead of functions to export data from MATLAB to new rows in a database. See Visual Query Builder for details. |
You learn to use these Database Toolbox functions:
SampleDB
. Type
numeric
by typing
numeric
, which is needed to perform calculations on the data.
freight
column of data from the orders
table. To keep the example simple, import only three rows of data. Type
numrows
. Then calculate the average, assigning the result to the variable meanA
. Type
setdbprefs
, but instead use standard MATLAB operations to define it. Put the date in the first cell by typing
Avg_Freight_Cost
table you created earlier, Calc_Date
and Avg_Cost
. Assign the cell array containing the column names to the variable colnames
. Type
AutoCommit
flag for the database. The status of the AutoCommit
flag determines if the database data will be automatically committed or not. If the flag is off
, you can undo an update.
AutoCommit
flag using the get
function by typing
The AutoCommit
flag is set to on
so exported data will be automatically committed. In this example, keep the AutoCommit
flag on
; for a Microsoft Access database, this is the only option.
conn
is the connection object for the database to which you are exporting data. In this example, conn
is SampleDB
, which is already open. However, if you export to a different database that is not open, use the database
function to connect to it before exporting the data.
Avg_Freight_Cost
is the name of the table to which you are exporting data. In the insert
function, you also include the colnames
cell array and the cell array containing the data you are exporting, exdata
, both of which you defined in the previous steps. Note that you do not define the type of data you are exporting; the data is exported in its current MATLAB format.
Running insert
appends the data as a new record at the end of the Avg_Freight_Cost
table.
If you get an error, it may be because the table is open in design mode in Access (edit mode for other databases). Close the table in Access and repeat the insert
function. For example, the error might be
Avg_Cost
value was rounded to a whole number to match the properties of that field in Access.
Avg_Freight_Cost
table in Access because you will use it in the next example.
Viewing Information About the Imported Data | Replacing Existing Data in a Database from MATLAB |
© 1994-2005 The MathWorks, Inc.