Com 调用Matlab';s MLApp.MLAppClass.FEval来自F#

Com 调用Matlab';s MLApp.MLAppClass.FEval来自F#,com,matlab,f#,variant,comexception,Com,Matlab,F#,Variant,Comexception,Matlab提供了一个COM接口,支持远程执行任意函数(和代码段)。特别是,它有一个调用给定Matlab函数的Feval方法。此方法的第三个参数pvarArgOut具有COM类型VARIANT*,并作为以下类型的参数出现在Visual Studio F#editor中: pvarArgOut: byref<obj> 这段代码编译得很好,但在调用interp1时,我遇到一个COM异常: A first chance exception of type 'System.Reflecti

Matlab提供了一个COM接口,支持远程执行任意函数(和代码段)。特别是,它有一个调用给定Matlab函数的Feval方法。此方法的第三个参数pvarArgOut具有COM类型VARIANT*,并作为以下类型的参数出现在Visual Studio F#editor中:

pvarArgOut: byref<obj>
这段代码编译得很好,但在调用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))
无论是使用新的obj、新的Array2D还是null初始化yi,我都会遇到相同的错误

F#如何翻译变量输出参数

更新

以下是更正的版本:

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[,]
StrangeLights.com上的文章介绍了如何使用F#v1.1.5和F#PowerPack中的MATLAB

缺少的步骤是使用tlbimp eg创建互操作性dll

tlbimp“c:\Program Files\MATLAB\R2006a\bin\win32\mlapp.tlb”

然后在F#中,使用


“#r”Interop.MLApp.dll”

您不想要
ref yi
,您想要

let mutable yi = new obj()
thatfunc(..., &yi, ...)

虽然我认为单靠这一点无法解决问题。可能有调用这个特定API的C#示例吗?

谢谢Matt;我看过这篇文章-这是一篇好文章,它让我开始了,但它没有涵盖Feval的使用。我已经创建了互操作性DLL,并将其添加为项目参考,所以这不可能是它…哦,很抱歉,我认为它这么简单。我刚刚快速查看并复制了该行为,但还没有找到更接近解决方案的方法。这很有效!-只需稍加修改即可将yi初始化为null,而不是新的obj()。调用之后,yi变量包含一个对象数组,第一个元素设置为二维双数组。完美的谢谢你,布莱恩!您好,Matthew,您能添加完整的工作示例吗?提前谢谢。
let mutable yi = new obj()
thatfunc(..., &yi, ...)