User`s guide

6 Using Database Toolbox Functions
6-22
Exporting Multiple Records from the MATLAB Workspace
This example does the following:
1
Imports monthly sales figures for all products from the dbtoolboxdemo data source
into the MATLAB workspace.
2
Computes total sales for each month.
3
Exports the totals to a new table.
You use the following Database Toolbox functions:
fastinsert
setdbprefs
For details about these functions, see matlab\toolbox\database\dbdemos
\dbinsert2demo.m.
1
Ensure that the dbtoolboxdemo data source is writable, that is, not read only.
2
Use the database function to connect to the data source, assigning the returned
connection object as conn. Pass the following arguments to this function:
dbtoolboxdemo, the name of the data source
username and password, which are passed as empty strings because no user
name or password is required to access the database
conn = database('dbtoolboxdemo', '', '');
Alternatively, you can use the native ODBC interface for an ODBC connection. For
details, see database.
3
Use the setdbprefs function to specify preferences for the retrieved data. Set the
data return format to numeric and specify that NULL values read from the database
are converted to 0 in the MATLAB workspace.
setdbprefs...
({'NullNumberRead';'DataReturnFormat'},{'0';'numeric'})
When you specify DataReturnFormat as numeric, the value for NullNumberRead
must also be numeric.
4
Import data from the salesVolume table.
curs = exec(conn, 'select * from salesVolume');