Deploy Relational Database Application with MATLAB Compiler
This example shows how to write a script to analyze data stored in a relational database, and deploy the script as a standalone application. Write code that connects to a database, imports data from the database into MATLAB®, analyzes the data, and closes the database connection. Then, you can deploy the code by compiling it as a standalone application by using the Application Compiler (MATLAB Compiler) app and running the application on other machines.
The example uses a JDBC driver to create the database connection. For a JDBC driver, include the JDBC driver JAR file among the files installed with your application. For an ODBC driver, install the ODBC driver and configure an ODBC data source on each machine where you run the application. For details about configuring ODBC and JDBC drivers, see Configure Driver and Data Source.
Overall, the example follows the steps described in Create Standalone Application from MATLAB (MATLAB Compiler) and updates the steps for a standalone database application.
Ensure that you have administrator privileges on the other machines to run the standalone application.
Create Function in MATLAB
Write a MATLAB script named
importAndAnalyzeDataFromDatabase.m and save it in a file location of your choice. The script contains the
importAndAnalyzeDataFromDatabase function, which returns the maximum product number from the data in the
productTable database table. The function connects to a Microsoft® SQL Server® database and imports all data from
productTable. Then, the function calculates the maximum product number.
function maxProdNum = importAndAnalyzeDataFromDatabase % IMPORTANDANALYZEDATAFROMDATABASE The importAndAnalyzeDataFromDatabase % function connects to a Microsoft® SQL Server® database using a JDBC % driver, imports data from the database into MATLAB®, performs a simple % data analysis, and closes the database connection. %% % Connect to the database by using the |Vendor| name-value pair argument of % the database function to specify a connection to an |SQLServer| database. % Set the |AuthType| name-value pair argument to |Server|. For example, % this code assumes that you are connecting to a database named |dbname|, % on a database server named |sname|, with the user name |username|, the % password |pwd|, and the port number |123456|. conn = database('dbname','username','pwd', ... 'Vendor','Microsoft SQL Server','Server','sname', ... 'AuthType','Server','PortNumber',123456); %% % Import data from the |productTable| database table. tablename = 'productTable'; data = sqlread(conn,tablename); %% % Determine the highest product number among products. prodNums = data.productnumber; maxProdNum = max(prodNums); %% % Close the database connection. close(conn) end
Create Standalone Application Using Application Compiler App
On the MATLAB Apps tab, on the far right of the Apps section, click the arrow to open the apps gallery. Under Application Deployment, click Application Compiler.
In the MATLAB Compiler project window, specify the main file of the MATLAB application that you want to deploy.
In the Main File section of the toolstrip, click .
In the Add Files dialog box, browse to the file location that contains your saved script. Select
importAndAnalyzeDataFromDatabase.mand click Open. The Application Compiler app adds the
importAndAnalyzeDataFromDatabasefunction to the list of main files.
Decide whether to include the MATLAB Runtime installer in the generated application by selecting one of the two options in the Packaging Options section:
Runtime downloaded from web — Generates an installer that downloads the MATLAB Runtime and installs it along with the deployed MATLAB application
Runtime included in package — Generates an installer that includes the MATLAB Runtime installer
Customize the packaged application and its appearance by entering the following options:
Application information — Editable information about the deployed application. You can also customize the appearance of the standalone application by changing the application icon and splash screen. The generated installer uses this information to populate the installed application metadata.
Additional installer options — Options for editing the default installation path for the generated installer and selecting a custom logo.
Files required for your application to run — Additional files required by the generated application to run. The software includes these files in the generated application installer.
Files installed for your end user — Files that are installed with your application. These files include the generated
readme.txtfile and the generated executable for the target platform. To create a database connection using the standalone application, add the driver JAR file. In this case, add
Additional runtime settings — Platform-specific options for controlling the generated executable.
For details about these options, see Customize an Application (MATLAB Compiler).
To generate the packaged application, click Package in the Package section on the toolstrip. In the Save Project dialog box, specify the location in which to save the project.
In the Package dialog box, verify that Open output folder when process completes is selected.
When the deployment process is complete, examine the generated output.
for_redistribution— Folder containing the file that installs the application and the MATLAB Runtime.
for_testing— Folder containing all the artifacts created by
mcc(such as binary, JAR, header, and source files for a specific target). Use these files to test the installation.
for_redistribution_files_only— Folder containing the files required for redistributing the application. Distribute these files to users who have MATLAB or MATLAB Runtime installed on their machines.
PackagingLog.txt— Log file generated by MATLAB Compiler™.
Install and Run Standalone Application
To install the standalone application, in the
for_redistribution folder, double-click the
If you want to connect to the Internet using a proxy server, click Connection Settings. Enter the proxy server settings in the provided dialog box. Click OK.
To complete the installation, follow the instructions in the installation wizard.
To run your standalone application:
Open a terminal window.
Navigate to the folder in which you installed the application.
Run the application.
Test Standalone Application on Target Machine
Choose one target machine to test the MATLAB generated standalone application.
Copy the files in the
for_testing folder to the target machine.
To test your standalone application:
Open a terminal window.
Navigate to the
Run the application.
Deploy Standalone Application on Target Machines
for_redistribution_files_only folder to a file location on all target machines where MATLAB or MATLAB Runtime is installed.
Run the MATLAB generated standalone application on all target machines by using the executable in the
- Create Functions in Files
- Create Standalone Application from MATLAB (MATLAB Compiler)
- Customize an Application (MATLAB Compiler)
- Import Data from Database Table Using sqlread Function