Database Toolbox Previous page   Next Page

Exporting Data Using the VQB

Build and run a query to export data from MATLAB into new rows in a database. Then save the query for use again later.

You cannot use the VQB to replace existing data in a database with data from MATLAB. Instead, use the update function. Use Database Toolbox functions instead of the VQB if you require commit and rollback features when exporting data.

Before You Start.   Before using the VQB, set up a data source--see Setting Up a Data Source. The examples here use the SampleDB data source.

To Start.   To open the VQB, in the Command Window, type

In the VQB, perform these steps to create and run a query to export data:

  1. In the Data operation field, select Insert, meaning you want to insert data into a database.
  2. From the Data source list box, select the data source into which you want to export data. For this example, select SampleDB, which is the data source for the Nwind database.
  1. After selecting a data source, the set of Tables in that data source appears.

  1. From the Tables list box, select the table into which you want to export data. For this example, select Avg_Freight_Cost. Table names that include spaces appear in quotation marks. For Microsoft Excel, the Tables are Excel sheets.
  1. After you select a table, the set of Fields (column names) in that table appears.

  1. From the Fields list box, select the fields into which you want to export data. To select more than one field, hold down the Ctrl key or Shift key while selecting. For this example, select the fields Calc_Date and Avg_Cost. Field names that include spaces appear in quotation marks. To deselect an entry, use Ctrl+click.
  1. As you select items from the Fields list, the query appears in the MATLAB Command field.

  1. Assign the data you want to export to a variable. For this example, type the following in the Command Window.
  1. This cell array contains a date and a numeric value.

    If the data contains NULL values, specify the format they take. Select Query -> Preferences and specify Write NULL numbers from and Write NULL strings from. For more information about these preferences, see the property descriptions on the reference page for setdbprefs, which is the equivalent function for setting preferences.

  1. In the VQB MATLAB workspace variable field, enter the name of the variable whose data you want to export. For this example, use export_data. Press Enter or Return to view the MATLAB command that exports the data.
  2. Click Execute to run the query and export the data.
  1. The query runs and exports the data. In the Data area, information about the exported data appears.

    If an error dialog box appears, the query is invalid. For example, you cannot perform a query on table and field names that contain quotation marks.

  1. In Microsoft Access, view the Avg_Freight_Cost table to verify the results.

  1. Note that the Avg_Cost value was rounded to a whole number to match the properties of that field in Access.

  1. To save this query, select Query -> Save and name it export.qry. See Saving, Editing, Clearing Variables for, and Specifying Preferences for Queries.

Previous page  Retrieving BINARY and OTHER Java Data BOOLEAN (MATLAB logical) Data Next page

© 1994-2005 The MathWorks, Inc.