A (not yet complete) Type Provider for Matlab in the spirit of the R Type Provider
The goal of this project is to provide a smooth and efficient framework for F#-Matlab interactions. The goals for 1.0 include support for most Matlab functions and types in as well typed a manner as possible.
A simple version of our goal is now working fairly well. Give it a little play and let me know if you find any problems.
Add the following parameters to your Matlab 2013a shortcut:
matlab.exe -automation -desktop
This will allow you to keep a Matlab session open while you connect and disconnect with Visual Studio. If you don't do this then the Matlab Provider will launch a session when it starts which will close when the Type Provider is unloaded.
Then you must load the type provider. This is done either by an assembly reference, if you're using it in your project, or by specifying it like so (and the required namespaces) in your F# .fsx script:
#R "MatlabTypeProvider.dll"
open FSMatlab
open FSMatlab.InterfaceTypes
With the Matlab Type Provider you work primarily with handles to functions and variables. Standard .NET values are supported interchangeably as well in order to make marshaling data across a simple matter. Working this way prevents a lot of extra pushing data back-and-forth from Matlab when doing complex combinations of functions. You must also specify exactly how many output parameters you would like. This is because Matlab functions may opt not to calculate unused results.
open Toolboxes.matlab.elmat // Open Matlab ToolBox as Namespace
let m,n = M.size([|1.0;2.0;3.0;4.0;5.0|]) |> EGT2<double,double>
Here EGT2 stands for Execute-Get-Typed with two outputs. Because you are not being returned a variable handle this will automatically delete the temporary result. We also provide the E function which just executes and returns a handle:
open Toolboxes.matlab.elfun
use x = M.nthroot(9.0, 2.0) |> E1
Here x is a handle to an active variable in your Matlab instance. Note that it implements IDisposable and so when bound with the use keyword will automatically be deleted from Matlab when it leaves scope. You can use it as you see fit and delete it when you're done.
let x_val_obj = x.GetUntyped() // type is obj
let x_val_typed : double = x.Get() // type is double