Database Toolbox | ![]() ![]() |
Working with Cell Arrays in MATLAB
When you import data from a database into MATLAB, the data is stored in MATLAB cell arrays. You can then use MATLAB functions to work with the data. This section provides a few simple examples of how to work with cell arrays in MATLAB.
For more information on using cell arrays, see Chapter 13 of Using MATLAB.
Viewing Query Results
How you view query results depends on if you imported the data using the fetch
function or if you used the Visual Query Builder.
Importing Data Using the fetch Function
If you import data from a database to MATLAB using the fetch
function, MATLAB returns, for example
curs = Attributes: [] Data: {3x1 cell} DatabaseObject: [1x1 database] RowLimit: 0 SQLQuery: 'select freight from orders' Message: [] Type: 'Database Cursor Object' ResultSet: [1x1 sun.jdbc.odbc.JdbcOdbcResultSet] Cursor: [1x1 com.mathworks.toolbox.database.sqlExec] Statement: [1x1 sun.jdbc.odbc.JdbcOdbcStatement] Fetch: [1x1 com.mathworks.toolbox.database.fetchTheData]
To view the retrieved data and assign it to the workspace variable A
, type
A = curs.Data
For this example, MATLAB returns
A = [12.7500] [10.1900] [52.8400]
If the query results consist of multiple columns, you can view all the results for a single column using a colon (:). For example, if running a fetch
returns data with multiple columns, you view the results of column 2 by typing
curs.data(:,2)
MATLAB returns the data in column 2
ans = [1400] [2400] [1800] [3000] [4300] [5000] [1200] [3000] [3000] [ 0]
Importing Data Using the Visual Query Builder
If you use the Visual Query Builder to import data, you assign the workspace variable, in this example A
, using the Visual Query Builder and do not have to perform the above steps. Instead, just type the workspace variable name at the MATLAB prompt in the Command Window. For this example, type
A
A = [12.7500] [10.1900] [52.8400]
Viewing Results Shown as a Matrix
If the results do not fit in the limited display space available, MATLAB expresses them as an array. If for example, MATLAB returns these query results.
B = [122] 'Virgina Power' [123] 'North Land Trading' [124] [1x20 char] [125] 'Bush Pro Shop'
you can see the data in rows 1, 2, and 4, but the second column in row 3 is expressed as an array because the results are too long to display.
To view the contents of the second column in the third row, type
B(3,2)
ans = 'The Ristuccia Center'
![]() | Performing Driver Functions | Retrieving Elements of Query Results | ![]() |