Main Content

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 Standalone 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.

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.

type importAndAnalyzeDataFromDatabase.m
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 Standalone Application Compiler.

After you open the app, the Create Compiler Task dialog box prompts you to add a compiler task to a new or an existing MATLAB® project. For this example, select Start a new project and create a compiler task and create a new project in your working folder. For more information on creating and using MATLAB® projects, see Create Projects.

A new compiler task named StandaloneDesktopApp1 opens in the Editor. You can compile code for other deployment targets by opening the Compiler Task Manager app or going to the Manage Tasks tab and creating a new compiler task.

For this example, in the Main File section of the compiler task, click Add Main File and select importAndAnalyzeDataFromDatabase.m. In the Project panel, the file now has the labels Design and Main Function. To set up the rest of the standalone application, see Specify Build Options (MATLAB Compiler).

See Also

| |

Topics