This example shows how to write a script to analyze data stored in a PostgreSQL database using the PostgreSQL native interface, and deploy the script as a standalone application. Write code that connects to a database using the PostgreSQL native interface, 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 using the Application Compiler (MATLAB Compiler) app, and then running the application on other machines.
Overall, the example follows the steps described in Create Standalone Application from MATLAB (MATLAB Compiler) and updates the steps for a standalone database application.
You must have administrator privileges on each machine where you plan to run the standalone application.
Create a MATLAB script named
importPostgreSQLNative.m and save it in a file location of your choice. The script contains the
importPostgreSQLNative function, which returns the maximum product number from the data in the
productTable database table. The function connects to a PostgreSQL database and imports all data from
productTable. Then, the function calculates the maximum product number.
You must use the syntax with name-value pair arguments when you connect to the database using the
function maxProdNum = importPostgreSQLNative % IMPORTPOSTGRESQLNATIVE The importPostgreSQLNative function connects to a % PostgreSQL database using the PostgreSQL native interface, imports data % from the database into MATLAB®, performs a simple data analysis, and % closes the database connection. The database contains a table named % |productTable|. %% % Connect to the database by using name-value pair arguments of the % |postgresql| function to specify a connection to a PostgreSQL database. % For example, this code assumes that you are using the user name % |username|, password |pwd|, database |dbname|, database server |sname|, % and port number |5432|. conn = postgresql("username","pwd", ... "DatabaseName","dbname", ... "Server","sname", ... "PortNumber",5432); %% % 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
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 the plus sign button to add a main file.
In the Add Files dialog box, browse to the file location that contains your saved script. Select
importPostgreSQLNative.m and click Open. The Application Compiler app adds the
importPostgreSQLNative function 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 specifying 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.txt file and the generated executable for the target platform.
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 the option 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 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.
To install the standalone application, double-click the
MyAppInstaller_web executable in 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.
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.
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