Select Query > Generate MATLAB File to create a file that contains the equivalent Database Toolbox™ functions required to run an existing query that was created in VQB. Edit the file to include MATLAB® or related toolbox functions, as needed. To run the query, execute the file.
The following is an example of a file generated by VQB:
% Set preferences with setdbprefs. s.DataReturnFormat = 'cellarray'; s.ErrorHandling = 'store'; s.NullNumberRead = 'NaN'; s.NullNumberWrite = 'NaN'; s.NullStringRead = 'null'; s.NullStringWrite = 'null'; s.JDBCDataSourceFile = ''; s.UseRegistryForSources = 'yes'; s.TempDirForRegistryOutput = ''; s.FetchInBatches = 'yes'; s.FetchBatchSize = '10000' setdbprefs(s) % Make connection to database. Note that the password has been omitted. % Using ODBC driver. conn = database('dbtoolboxdemo','','password'); % Read data from database. e = exec(conn,'SELECT ALL StockNumber,January,February FROM salesVolume'); e = fetch(e); close(e) Close database connection. close(conn)
The following VQB query elements do not appear in generated files:
Generated code files do not include MATLAB workspace
variables to which you assigned query results in the VQB query. The
file assigns the query results to
e; access these
results using the variable
e.Data. For example,
you can add a statement to the file that assigns a variable name to
myVar = e.Data
For security reasons, generated files do not include
passwords required to connect to databases. Instead, the
database statement includes the string
a placeholder. To run files to connect to databases that require passwords,
substitute your password for the string
Click Query > Save. The Save SQL Statement dialog box appears.
Enter a name (without spaces) for the
query into the File name field and click Save.
Save the query as
Note: When you save a Select query (a query that imports data), MATLAB does not save your specified preferences or the workspace variable that contains the query results. This prevents you from inadvertently overwriting an existing variable in the MATLAB workspace when you run a saved query.
When you save an Insert query (a query that exports data), MATLAB saves the workspace variable whose data you exported, but does not save your preferences.
Click Query > Load. The Load SQL Statement dialog box appears.
Select the name of the query you want to load and click Open. The VQB fields reflect the values for the saved query.
Run a Select query to import data into the MATLAB workspace, or an Insert query to export data from the MATLAB workspace.
To run a Select query, use the MATLAB workspace variable field to assign a variable to the data and click Execute.
For an Insert query, the saved query may include a workspace variable, which appears as part of the MATLAB command field. Type that variable name or enter a new name in the MATLAB workspace variable field. Press Return or Enter to see the updated MATLAB command.
Click Execute to run the query.
Tip You can generate a file that runs the query from the Command Window in the future. For details, see Saving Queries in Files in the Database Toolbox documentation.