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 MATLAB Data Types in the MATLAB Programming documentation.
You might also need more information about working with strings in MATLAB. See the functions char
, cellstr
, and strvcat
and Characters and Strings in the MATLAB Programming documentation.
Viewing Cell Array Data Returned from a Query
Viewing Query Results
How you view query results depends on if you import the data using the fetch
function or if you use the Visual Query Builder.
Using the fetch Function. If you import data using the fetch
function, MATLAB returns data to a cursor object, as in the following data, which was imported in the example 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
Using the Visual Query Builder. If you import data using the Visual Query Builder, you assign the results to the workspace variable, which is A
in this example, using the VQB. To see the data, type the workspace variable name at the MATLAB prompt in the Command Window, for example, type A
.
MATLAB displays the data in the Command Window, for example
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 displays size information only. If, for example, MATLAB returns these query results.
You can see the data in columns 1 and 2, but the third is expressed as an array because the results are too long to display.
To view the contents of the third column in the first 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 |
© 1994-2005 The MathWorks, Inc.