Database Toolbox | ![]() ![]() |
Working with Cell Arrays in MATLAB
When you import data from a database into MATLAB, the data is stored as a numeric matrix, a structure, or a MATLAB cell array, depending on the data return format preference you specified using setdbprefs
or the Database Toolbox Preferences dialog box.
Once the data is in MATLAB, you can use MATLAB functions to work with it. Because some users are unfamiliar with cell arrays, this section provides a few simple examples of how to work with the cell array data type in MATLAB:
For more information on using cell arrays, see Structures and Cell Arrays in the MATLAB documentation.
Viewing Cell Array Data Returned from a Query
Viewing Query Results
fetch
function or if you used the Visual Query Builder.
fetch
function, MATLAB returns, for example (see Exporting Data from MATLAB to a New Record in a Database)
curs = Attributes: [] Data: [3x1 double] 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]
The retrieved data is in the field Data. To view it, type
Alternatively, you can assign the data to a variable, for example, A
, by typing
A
in this example, using the Visual Query Builder. To see the data, type the workspace variable name at the MATLAB prompt in the Command Window, for example, type A
.
Viewing Results with Multiple Columns
If the query results consist of multiple columns, you can view all the results for a single column using a colon (:). See the example in Exporting Multiple New Records from MATLAB. For example, you view the results of column 2 by typing
or if you used fetch
, you can also view it by typing
MATLAB returns the data in column 2, for example
Expanding Results
If the results do not fit in the display space available, MATLAB expresses them as an array. If for example, MATLAB returns these query results.
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
or if you used fetch
, you can also view it by typing
![]() | About Objects and Methods for the Database Toolbox | Viewing Elements of Cell Array Data | ![]() |