Quick Start Implementation of WebFigures

Assumptions About the Example

To work with this example:

  • Assume the following MATLAB® function has been created:

    function df = getPlot()
      f = figure('Visible','off');
      x = -2:0.25:2;
      [X,Y] = meshgrid(x);
      Z = X.*exp(-X.^2-Y.^2);
      contour3(X,Y,Z,30);
      df = webfigure(f);
      close(f);
    end
  • Assume that the function getPlot has been deployed in a .NET assembly with a namespace of MyComponent and a class of MyComponentclass.

  • Assume the MATLAB Runtime has been installed. If not, refer to Install MATLAB Runtime.

  • If you are running on a system with 64-bit architecture, use the information in Advanced Configuration of a WebFigure to work with WebFigures unless you are deploying a website which is 32-bit only and you have a 32-bit MATLAB Runtime installed.

Procedure

To implement WebFigures in .NET for MATLAB Compiler SDK™ using the Quick Start approach, do the following:

  1. Start Microsoft® Visual Studio®.

  2. Select File > New > Web Site to open.

  3. Select one of the template options and click OK.

      Caution   Do not select Empty Web Site as it is not possible to create a WebFigure using this option.

  4. In your Visual Studio project, add a reference to matlabroot\toolbox\dotnetbuilder\bin\arch\v2.0\WebFiguresService.dll.

    If you are using the MATLAB Runtime, matlabroot is the location of the installed MATLAB Runtime.

    If you are using MATLAB Compiler SDK, matlabroot is the location of the installed MATLAB Compiler SDK.

      Note:   If you are running on a system with 64-bit architecture, use the information in Advanced Configuration of a WebFigure to work with WebFigures unless you are deploying a website which is 32-bit only and you have a 32-bit MATLAB Runtime installed.

  5. Install WebFigureControl Into Microsoft Visual Studio Toolbox.

  6. Drag the WebFigureControl from the toolbox to your web page. After dragging, the web page displays the following default figure.

    You can resize the control as you would any other .NET web control.

  7. Switch to the Design view in Microsoft Visual Studio by selecting View > Designer.

  8. Test the web page by "playing" it in Microsoft Visual Studio. Select Debug > Start Debugging. The page should appear as follows.

  9. Interact with the default figure on the page using your mouse. Click one of the three control icons at the top of the figure to activate the desired control, select the desired region of the figure you want to manipulate, then click and drag as appropriate. For example, to zoom in on the figure, click the magnifying glass icon, then hover over the figure.

  10. Close the page as you would any other window, automatically exiting debug or "play" mode.

  11. The WebFigureService you created has been verified as functioning properly and you can attach a custom WebFigure to the web page:

    1. To enable return of the webfigure and to bind it to the webfigure control, add a reference to MWArray to your project and a reference to the deployed component you created earlier (in Assumptions About the Example). See Common Integration Tasks for more information.

    2. In Microsoft Visual Studio, access the code for the web page by selecting View > Code.

    3. In Microsoft Visual Studio, go to the Page_Load method, and add this code, depending on if you are using the C# or Visual Basic® language. Adding code to the Page_Load method ensures it executes every time the web page loads.

        Note:   The following code snippets belong to the partial classes generated by your .NET web page.

      • C#:

        using MyComponent;
        using MathWorks.MATLAB.NET.WebFigures;
        
        protected void Page_Load(object sender, EventArgs e)
        {
                MyComponentclass myDeployedComponent =
                        new MyComponentclass();
                WebFigureControl1.WebFigure =
                        new WebFigure(myDeployedComponent.getPlot());
        }
        
      • Visual Basic:

        Imports MyComponent
        Imports MathWorks.MATLAB.NET.WebFigures
        
        Protected Sub Page_Load(ByVal sender As Object,
                                ByVal e As System.EventArgs)
                                Handles Me.Load
            Dim myDeployedComponent As _
                 New MyComponentclass()
            WebFigureControl1.WebFigure = _
                 New WebFigure(myDeployedComponent.getPlot())
        End Sub
        

        Tip   This code causes the deployed component to be reinitialized upon each refresh of the page. A better implementation would involve initializing the myDeployedComponent variable when the server starts up using a Global.asax file, and then using that variable to get the WebFigure object. For more information on Global.asax, see Using Global Assembly Cache (Global.asax) to Create WebFigures at Server Start-Up.

        Note:   WebFigureControl stores the WebFigure object in the IIS session cache for each individual user. If this is not the desired configuration, see Advanced Configuration of a WebFigure for information on creating a custom configuration.

  12. Replay the web page in Microsoft Visual Studio to confirm your WebFigure appears as desired.

Was this topic helpful?