| Contents | Index |
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 summarizes how to integrate the deployable MATLAB magic function from The Magic Square Example in Getting Started of this user's guide.
You must be running Microsoft Visual Studio 2010 or higher to use this example.
For more information about the F# language, go to http://fsharp.net.
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.
Build the makeSqr component using the instructions in Creating a .NET Component From MATLAB Code in Getting Started of this user's guide.
Using Microsoft Visual Studio 2010 or higher, create an F# project.
Add references to your component and MWArray in Visual Studio. For examples of how to do this, see Creating a Reference to Your Component and Creating a Reference to the MWArray API in this user's guide.
Make the .NET namespaces available for your component and MWArray libraries:
open makeSqr open MathWorks.MATLAB.NET.Arrays
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.
Instantiate the Magic Square component:
use magicComp = new makeSqr.MLTestClass()
Define the input argument:
use inarg = new MWNumericArray((int) n)
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 makeSqr.MLTestClass()
// 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[,])
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:
See Distribute MATLAB Code Using the MATLAB Compiler Runtime (MCR) for information about deploying your component to end users.
![]() | Deploying .NET Components With the F# Programming Language |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |