Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Data Import Using Database Explorer App or Command Line

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.

Data Import Using Database Explorer App

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 runsqlscript function.

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.

Data Import Using Command Line

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 exec and 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.

FunctionalityOne-Step Data ImportTwo-Step Data Import
Function

select

exec and fetch

SQL statement

Single SELECT statement only

Single or multiple SELECT statements

Data types for imported numeric values

Specified by the database table definition

MATLAB double

Memory management

Integer classes for numeric values

Number of imported rows or database preferences

Database preferences

Setting database preferences not required

Setting database preferences required using setdbprefs

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.

If you have a stored procedure that imports data, then use the runstoredprocedure or exec function.

Custom Data Types

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.

SQL Queries Saved in Scripts or Files

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.

See Also

| | |

Related Topics

Was this topic helpful?