User`s guide

6 Using Database Toolbox Functions
6-4
Import Data from Databases into MATLAB
This example shows how to import data from a Microsoft Access database called
dbtoolboxdemo into the MATLAB workspace.
Connect to the Database
Connect to the Microsoft Access database with the data source name dbtoolboxdemo
using native ODBC.
conn = database.ODBCConnection('dbtoolboxdemo','','');
If you are connecting to a database using a JDBC connection, then specify a different
syntax for the database function.
Import Data Using a Simple SQL Query
Select the product number productNumber and description productDescription from
the product table productTable. Create an SQL query to select this data. Then, use the
exec function to execute the SQL query using the database connection object conn.
sqlquery = 'select productNumber,productDescription from productTable';
curs = exec(conn,sqlquery);
The data contains strings. Set the data return format to support strings. Use the
setdbprefs function to specify the format cellarray.
setdbprefs('DataReturnFormat','cellarray')
Display the data. Use the fetch function to fetch the data from the executed SQL query.
curs = fetch(curs);
curs.Data
ans =
[ 9] 'Victorian Doll'
[ 8] 'Train Set'
[ 7] 'Engine Kit'
[ 2] 'Painting Set'
[ 4] 'Space Cruiser'
[ 1] 'Building Blocks'
[ 5] 'Tin Soldier'
[ 6] 'Sail Boat'