Matlab は、任意の関数 (およびコード スニペット) のリモート実行をサポートする COM インターフェイスを提供します。特に、特定の Matlab 関数を呼び出す Feval メソッドがあります。このメソッドの 3 番目のパラメーターである pvarArgOut は COM 型 VARIANT* を持ち、Visual Studio F# エディターでは次の型の引数として表示されます。
pvarArgOut: byref<obj>
次のコードは interp1 を呼び出します。これは、ほとんどの Matlab 関数で通常行われているように、Matlab では行列 (つまり、2D double 配列) の結果を返します。
let matlab = new MLApp.MLAppClass()
let vector_to_array2d (v : vector) = Array2D.init v.Length 1 (fun i j -> v.[i])
let interp1 (xs : vector) (ys : vector) (xi : vector) =
let yi : obj = new obj()
matlab.Feval("interp1", 1, ref yi, vector_to_array2d xs, vector_to_array2d ys, vector_to_array2d xi)
yi :?> float[,]
このコードは正常にコンパイルされますが、interp1 を呼び出すと、COM 例外が発生します。
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll
Additional information: Invalid callee. (Exception from HRESULT: 0x80020010 (DISP_E_BADCALLEE))
yi を新しい obj、新しい Array2D、または null で初期化しても、同じエラーが発生します。
F# は VARIANT 出力引数をどのように変換しますか?
アップデート
修正版は次のとおりです。
let matlab = new MLApp.MLAppClass()
let vector_to_array2d (v : vector) = Array2D.init v.Length 1 (fun i j -> v.[i])
let interp1 (xs : vector) (ys : vector) (xi : vector) =
let mutable oi : obj = null
matlab.Feval("interp1", 1, &oi, vector_to_array2d xs, vector_to_array2d ys, vector_to_array2d xi)
(oi :?> obj[]).[0] :?> float[,]