Quantcast

Documentation Center

  • Trial Software
  • Product Updates

Call MATLAB Function from Web Application

This example shows you how to create a Web page that uses a MATLAB® application as an Automation server. Run this example from a local system; you cannot deploy it from a Web server. For another example using ASP.NET, see http://www.mathworks.com/matlabcentral/answers/99541-how-do-i-leverage-the-matlab-engine-to-deploy-my-matlab-code-over-the-web-for-an-asp-net-application.

You can invoke MATLAB as an Automation server from any language that supports COM, so for Web applications, you can use VBScript and JavaScript. While this example is simple, it illustrates techniques for passing commands to MATLAB and writing data to and retrieving data from the MATLAB workspace. See Exchanging Data with the Server for related functions.

VBScript and HTML forms are combined in this example to create an interface that enables the user to select a MATLAB plot type from a pull-down menu, click a button, and create the plot in a MATLAB figure window. To accomplish this, the HTML file contains code that:

  • Starts MATLAB as an Automation server via a VBScript.

  • When users click a button on the HTML page, a VBScript executes that:

    1. Determines the type of plot selected.

    2. Forms a command string to create the type of plot selected.

    3. Forms a string describing the type of plot selected, which passes to the MATLAB base workspace in a variable.

    4. Executes the MATLAB command.

    5. Retrieves the descriptive string from the MATLAB workspace.

    6. Updates the text box on the HTML page.

Here is the HTML used to create this example:

<HTML>
<HEAD>
<TITLE>Example of calling MATLAB from VBScript</TITLE>
</HEAD>
<BODY>
<FONT FACE = "Arial, Helvetica, Geneva" SIZE = "+1" COLOR = "maroon">
Example of calling MATLAB from VBScript
</FONT>
<FONT FACE = "Arial, Helvetica, Geneva" SIZE = "-1">

<!-- %%%%%%%%%%%%%%%%%%%% BEGIN SCRIPT %%%%%%%%%%%%%%%%%%%% -->
<SCRIPT LANGUAGE="VBScript">
<!-- Invoke MATLAB as a COM Automation server upon loading page
' Initialize global variables
Dim MatLab	'COM Automation server variable
Dim MLcmd	'string to send to MATLAB for execution
' Invoke COM Automation server
Set MatLab = CreateObject("Matlab.Application")
' End initialization script -->	
</SCRIPT>

<!-- %%%%%%%%%%%%%%%%%%%% END SCRIPT %%%%%%%%%%%%%%%%%%%% -->
<!-- Create form to contain controls -->
<FORM NAME="Form">
<!-- Create pulldown menu to select which plot to view -->
<P>Select type of plot:
<SELECT NAME=plot_choice>
	<OPTION SELECTED VALUE=first>Line</OPTION>
	<OPTION VALUE=second>Peaks</OPTION>
	<OPTION VALUE=third>Logo</OPTION>
</SELECT>
<!-- Create button to create plot and fill text area -->
<P>Create figure:
<INPUT TYPE="button" NAME="plot_but" VALUE="Plot">

<!-- %%%%%%%%%%%%%%%%%%%% BEGIN SCRIPT %%%%%%%%%%%%%%%%%%%% -->
<SCRIPT FOR="plot_but" EVENT="onClick" LANGUAGE="VBScript">
<!-- Start script 
Dim plot_choice
Dim text_str  	'string to display in text area
Dim form_var	'form object variable
Set form_var = Document.Form
plot_choice = form_var.plot_choice.value
' Condition MATLAB command to execute based on plot choice
If plot_choice = "first"	Then
   MLcmd = "figure; plot(1:10);" 
   text_str = "Simple line plot of 1 to 10"
   Call MatLab.PutCharArray("text","base",text_str)
Elseif plot_choice = "second" Then
   MLcmd = "figure; mesh(peaks);" 
   text_str = "Mesh plot of peaks"
   Call MatLab.PutCharArray("text","base",text_str)
Elseif plot_choice = "third" Then
   MLcmd = "figure; logo;" 
   text_str = "MATLAB logo"
   Call MatLab.PutCharArray("text","base",text_str)
End If
' Execute command in MATLAB
MatLab.execute(MLcmd)
' Get variable from MATLAB into VBScript
Call MatLab.GetWorkspaceData("text","base","text_str")
' Update text area	
form_var.plottext.value = text_str
' End script -->
</SCRIPT>

<!-- %%%%%%%%%%%%%%%%%%%% END SCRIPT %%%%%%%%%%%%%%%%%%%% -->
<!-- Create text area to show text -->
<P><TEXTAREA NAME="plottext" ROWS="1" COLS="50" 
CONTENTEDITABLE="false"></TEXTAREA> 
</FONT>
</FORM>
</BODY>
</HTML>
Was this topic helpful?