Main Content

Integrate .NET Assembly Into F# Application

The F# programming language offers the opportunity to implement the same solutions you usually implement using C#, but with less code. This can be helpful when scaling a deployment solution across an enterprise-wide installation, or in any situation where code efficiency is valued. The brevity of F# programs can also make them easier to maintain.

The following example shows you how to integrate the deployable MATLAB magic function into an F# application.

Prerequisites

You must be running Microsoft® Visual Studio® 2010 or higher to use this example.

If you build this example on a system running 64-bit Microsoft Visual Studio, you must add a reference to the 32-bit MWArray DLL due to a current imitation of Microsoft's F# compiler.

Step 1: Build the Component

Build the MagicSquareComp component using the instructions in Generate .NET Assembly and Build .NET Application.

Step 2: Integrate Component Into F# Application

  1. Using Microsoft Visual Studio 2010 or higher, create an F# project.

  2. Add references to your .NET component and MWArray in Visual Studio.

  3. Make the .NET namespaces available for your component and MWArray libraries:

    open MagicSquareComp
    open MathWorks.MATLAB.NET.Arrays
    
  4. Define the Magic Square function with an initial let statement, as follows:

    let magic n =
    Then, add the following statements to complete the function definition.

    1. Instantiate the Magic Square component:

      use magicComp = new MagicSquareComp.MagicSquareClass()  
    2. Define the input argument:

      use inarg = new MWNumericArray((int) n)
      
    3. Call MATLAB®, get the output argument cell array, and extract the first element as a two–dimensional float array:

      (magicComp.makesquare(1, inarg).[0].ToArray() :?> float[,])
      

    The complete function definition looks like this:

    let magic n =
        // Instantiate the magic square component
        use magicComp = new MagicSquareComp.MagicSquareClass()
        // Define the input argument
        use inarg = new MWNumericArray((int) n)
        // Call MATLAB, get the output argument cell array,
        // extract the first element as a 2D float array
        (magicComp.makesquare(1, inarg).[0].ToArray() 
                                         :?> float[,])
    
  5. Add another let statement to define the output display logic:

    let printMagic n = 
      let numArray = magic n
      // Display the output
      printfn "Number of [rows,cols]: [%d,%d]" 
          (numArray.GetLength(0)) (numArray.GetLength(1))
      printfn ""
      for i in 0 .. numArray.GetLength(0)-1 do
        for j in 0 .. numArray.GetLength(1)-1 do
          printf "%3.0f " numArray.[i,j]
        printfn ""
      printfn "=========================\n"
    
    ignore(List.iter printMagic [1..19])
    // Pause until keypress
    ignore(System.Console.ReadKey())

The complete program listing follows:

 The F# Magic Square Program

Step 3: Deploy the Component

For information about deploying your component to end users, see MATLAB Runtime.