You can import data from a database into MATLAB® using the Database Explorer app or the command line. To select data for import, you can build an SQL query visually by using the Database Explorer app. Or, you can use the command line to write SQL queries. To achieve maximum performance with large data sets, use the command line instead of the Database Explorer app.
After importing data, you can repeat the steps in the process, such as connecting to a database, executing an SQL query, and so on, by using a MATLAB script to automate them.
To open multiple connections to the same database simultaneously, you can create multiple SQL queries using the Database Explorer app. Or, you can connect to the database using the command line.
If you do not have access to a database and want to import your data quickly, you can use the MATLAB interface to SQLite. For details, see Working with MATLAB Interface to SQLite.
If you have minimal proficiency writing SQL queries or want to browse the data in a database
quickly, use the Database Explorer app. To build
queries, see Create SQL Queries Using Database Explorer App. After
creating a query using the Database Explorer app, you can generate the SQL code for
the query. For details, see Generate SQL Query. You can
embed the generated SQL code into the SQL query that you specify in the
exec function. Or, you can create
an SQL script file to use with the
If you want to automate the current task after you create the SQL query, then generate a MATLAB script. For details, see Generate MATLAB Script.
If you know how to write SQL queries, use the command line to explore a database. You can write basic SQL statements as character vectors or string scalars. For a simple example, see Import Data from Databases into MATLAB. If you have variables in the MATLAB workspace, you can add them to the SQL query. For an example, see Create Queries with Characters and Variables.
You can import data into MATLAB in one of two ways. Use the
select function for maximum memory
efficiency and quick access to imported data in one step. Use the
fetch functions for a two-step
approach with maximum flexibility for setting database preferences and importing
numeric data with double precision. The
exec function executes
the SQL statement, and the
fetch function imports the data from
the database into a MATLAB variable. This table provides details about the two ways to import
data into MATLAB.
|Functionality||One-Step Data Import||Two-Step Data Import|
Single or multiple
|Data types for imported numeric values||
Specified by the database table definition
Integer classes for numeric values
Number of imported rows or database preferences
Setting database preferences not required
Setting database preferences required using
For memory management, see Data Import Approaches and Memory Management.
If you are not comfortable writing SQL queries, then use the Database Explorer app to select data to import from your database.
When importing data from a database, Database
Toolbox™ functions return custom data types, such as Oracle® ref cursors, as Java® objects. You can manually parse these objects to retrieve their data
contents. Use the
methods function to access all the
methods of a Java object. Use the available methods to retrieve data from a Java object. The steps for your object are specific to your database. For
details, refer to your JDBC driver or database documentation.
If you have a long SQL query or multiple SQL queries that you want to run sequentially to
import data, create an SQL script file containing your SQL queries. To execute the
SQL script file, use the
runsqlscript function. If you have
SQL queries stored in
.sql or text files that you want to run
from MATLAB, you also can use this function.