Web Deployment of MATLAB Applications Guide
As of R2006b, the MATLAB Web Server product has been discontinued. Going forward, there are several options for making your MATLAB applications available via the Web. Each of the examples below contains code that you may use as a template, along with a readme file describing the steps required to create the application. We will continue to add links to more examples over time.
The Software License Agreement has been modified to state MATLAB users on concurrent licenses or "designated computer" individual licenses (installed on a single computer) may access their MATLAB applications from web pages. At no time is it legal to access the MATLAB command prompt from a web page.
Contents
- Recommended Deployment Options
- Using MATLAB Builder for .NET to create a server-side COM or .NET component
- Using MATLAB Builder for Java to create a Java servlet or a Web Service
- Calling MATLAB functions via CGI
- Accessing MATLAB algorithms directly from the Web, using COM automation, or other technologies
Section 1: Recommended Deployment Options
You may choose to create Web applications that access MATLAB directly (hosting MATLAB on the application server), or by building executables or components using the MATLAB deployment products (MATLAB Compiler, MATLAB Builder for Java and MATLAB Builder for .NET). Deployed applications do not require MATLAB, and may be deployed royalty-free to as many servers as you wish.
The following table lists recommended deployment options for each platform, but other options are possible.
Platform | Application Server Does Not have MATLAB | Application Server Does have MATLAB |
---|---|---|
Windows | MATLAB Builder for .NET MATLAB Builder for Java | MATLAB COM Automation |
Linux/Unix | MATLAB Builder for Java | CGI with MATLAB as an Executable |
MAC | CGI with an Executable Built by MATLAB Compiler | CGI with MATLAB as an Executable |
Section 2: Using MATLAB Builder for .NET to create a server-side COM or .NET component
Builder for .NET requires MATLAB and the MATLAB Compiler. The examples below use .NET or COM components from Builder for .NET with ASP.NET, which requires IIS.
Sudoku Web Application (ASP.NET)
- SOLVE_SUDOKU.M -- This file contains the code for solving the Sudoku board, and should be deployed as a .NET assembly. It accepts as input a 9-by-9 array, where 0 represents a blank square, and the digits 1-9 represent any initial values in the grid. The function returns the solved board, if a solution exists, and a flag indicating whether the code was successful at finding a solution. To solve the puzzle, the code uses an algorithm that makes recursive calls to the solver function after choosing one of the possible candidates for a square. If a square does not have any possible candidates, the algorithm backs up to a previous square, and tries another possible candidate there before proceeding.
- SUDOKU.ASPX -- The front-end Web page is an ASP.NET file that creates a 9-by-9 grid of text boxes, allowing users to enter the values for a Sudoku puzzle. After a user clicks the Submit button, this code posts the data in the grid to solver.aspx.
- SOLVER.ASPX -- This ASP.NET code accepts the input data from sudoku.aspx, packages it in a .NET array, and calls a method of the Sudoku object to solve the puzzle. After receiving the output in the form of a two-dimensional array, the ASP.NET code formats the array to display the solution on the screen.
For more information on solving Sudoku puzzles, check out the following reference:
Delahaye, Jean-Paul. "The Science Behind Sudoku."
Scientific American, June 2006: 81-83.
Note: Certain boards take longer to solve than others. It may be helpful to set the timeout for the ASP.NET page appropriately for boards that take an excessive amount of time to complete.
You may download the MATLAB Builder for .NET Sudoku files from MATLAB Central:
Sudoku: ASP.NET/MATLAB
http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=12096&objectType=file
Surf Peaks Example Using .NET
http://www.mathworks.com/support/solutions/data/1-2EETRY.htmlThe following sample applications require additional toolboxes:
Black-Scholes Option Value application (either .NET or COM components):
http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=12098&objectType=file
Gene Expression Web application using .NET:
http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=10344&objectType=fileGene Expression Web application using COM:
http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=9418&objectType=file
General pointers for using ASP.NET with MATLAB
Here are some pointers for ASP.NET deployment, particularly for customers transitioning from MATLAB Web Server:
- In M-code, you need to use the print command to save the figure to a JPEG format as opposed to wsprintjpeg.
- You must ensure that IIS points to the correct version of ASP.NET (as selected during compilation). After publishing the ASP.NET application to the web using the Publish to Web option in Visual Studio, setting the correct version is accomplished by:
- Select Start > Control Panel > Administrative Services > Internet Information Services.
- Drill down from Local Computer > Web Sites > WebApp, where WebApp corresponds to the virtual directory that points to the ASP.NET application's project folder.
- Right-click this directory and select the Properties option.
- Click the ASP.NET tab and specify the appropriate version using the dropdown menu.
If this step is not performed, the application displays an error pointing to the web.config file (depends on your application).
- MATLAB Web Server HTML tags needed modification for ASP.NET/design mode compatibility:
tags caused errors for design mode, and the page would not display in this mode. Errors were very helpful in providing guidance on needed changes.
You are required to convert HTML controls to ASP control format (drop-down menus, text fields, etc.). For example, an HTML text field is expressed as follows:
<input type="text" size="5" name="AZ" maxlength="5" value="-37.5" ID="Text1">
Its ASP control equivalent is expressed as:
<asp:Textbox id="AZ" runat="server" Width="60px">-37.5"</asp:Textbox>
- If prohibiting client write access for the virtual directory containing the ASP.NET web application, pre-extraction of the CTF-archive file is necessary.
Section 3: Using MATLAB Builder for Java to create a Java servlet or a Web Service
Our sample Builder for Java applications use Java front ends with the Tomcat servlet container/Web server.
MATLAB Builder for Java: Web Surface Peaks example using a JAVA Component
The following sample application requires additional toolboxes:
Web Black-Scholes Option Value (JAVA component)
There is also a Web Services example using Builder for Java, with no toolboxes from File Exchange on MATLAB Central.
Section 4: Calling MATLAB functions via CGI
MATLAB can be called directly as an executable via CGI, or executables created with the MATLAB Compiler can be created and deployed. We currently have Technical Support example solutions available on our website:
Can I create a CGI executable that calls a shared library using the MATLAB Compiler to deploy my application on the Web?
http://www.mathworks.com/support/solutions/data/1-2YVMOV.shtml
Can I create a standalone CGI executable to deploy my application on the Web using the MATLAB Compiler?
http://www.mathworks.com/support/solutions/data/1-30REEY.shtml
Section 5: Accessing MATLAB algorithms directly from the Web, using COM automation, or other technologies
- Call MATLAB through its COM automation interface, similarly to how you would call a COM object from MATLAB Builder for .NET.
- Wrap the MATLAB COM automation interface as a Web service:
Implementation of MATLAB Web Services Interface:
This thin wrapper uses the MATLAB functions PutWorkspaceData and GetWorkspaceData to pass data values, and then passes a string of XML to MATLAB to be processed, so the user can access MATLAB functions they have written.
Note that the MathWorks Software License Agreement specifically forbids exposing the MATLAB command line, i.e. allowing the end users of the Web site to define the XML string to execute. Instead, this approach must be used in the context of an application, executing specific user-created M-functions.
[WebService(Namespace = "http://www.mathworks.com/demo")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
MLApp.MLAppClass myMATLAB;
public Service () {
// Start MATLAB
myMATLAB = new MLApp.MLAppClass();
}
[WebMethod]
public string ProcessXml(string aInputXml)
{
object outputXml;
// Put data into MATLAB
myMATLAB.PutWorkspaceData("transIn","base", , aInputXml);
// Execute our UserDefined function
myMATLAB.Execute("TxRequest");
// Retrieve the result
myMATLAB.GetWorkspaceData("transOut", "base", out outputXml);
return outputXml.ToString();
}
}
One key aspect in the design is the timing of when to start up MATLAB and when to call the commands to process the data. As shown above, we initialized our MATLAB application when the service was initiated. This causes MATLAB to start if a copy was not already running with the automation on. Then when the user calls ProcessXml only the data communication needs to occur.
If you plan to call the ProcessXml function often and expect accessing this function simultaneously from multiple threads, you need to take further action to handle the locking of access to MATLAB while in this method.
Calling Perl Programs from MATLAB
On Windows, it is also possible to call MATLAB from PHP or Perl, using COM automation. Perl supports using MATLAB arrays as detailed below. PHP can only accept scalar values from MATLAB due to the limitations of its COM array implementation. Details and example code are included in a biology demo on MATLAB Central.