Database Toolbox | ![]() ![]() |
Exporting Data from MATLAB, Replacing Existing Data in a Database
In this part of the tutorial, you export data from MATLAB to a database, updating existing data in the database. Specifically, you update the data you previously imported into the Avg_Freight_Cost
table.
You use these Database Toolbox functions:
If you want to see or copy the functions for this part of the tutorial, or if you want to run the set of functions, use the M-file matlab\toolbox\database\dbdemos\dbupdatedemo.m
.
conn = database('SampleDB', '', ''); colnames = {'Calc_Date', 'Avg_Cost'}; D = '1/20/98'; meanA = 25.2600; exdata = {D, meanA}
Avg_Freight_Cost
table is incorrect and instead should be 1/19/98. TypeD = '1/19/98'
exdata
, which contains the data you will export. Typeexdata(1,1) = {D}
where
statement and assign it to the variable whereclause
. The record to be updated is the record that has 1/20/98
for the Calc_Date
.whereclause = 'where Calc_Date = ''1/20/98'''
Because the date string is within a string, two single quotation marks surround the date instead of the usual single quotation mark. MATLAB returns
whereclause = where Calc_Date = '1/20/98'
Calc_Date
is 1/20/98
.update(conn, 'Avg_Freight_Cost', colnames, exdata, whereclause)
Avg_Freight_Cost
table to verify the results.close(conn)
Always close a connection when you are finished with it to avoid using memory unnecessarily and to ensure there are enough available connections for other users.
![]() | Exporting Data from MATLAB to a New Record in a Database | Exporting Multiple Records from MATLAB | ![]() |