MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   从F#调用Matlab的MLApp.MLAppClass.FEval (https://www.labfans.com/bbs/showthread.php?t=23421)

poster 2019-12-10 20:41

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

pvarArgOut: byref 以下代码调用interp1,在Matlab中,它返回矩阵(即2D双数组)结果,这与大多数Matlab函数一样。

let matlab = new MLApp.MLAppClass() let vector_to_array2d (v : vector) = Array2D.init v.Length 1 (fun ij -> 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)) 无论使用新的obj,新的Array2D还是null初始化yi,我都会遇到相同的错误。

F#如何转换VARIANT输出参数?

[I]更新资料[/I]

这是更正的版本:

let matlab = new MLApp.MLAppClass() let vector_to_array2d (v : vector) = Array2D.init v.Length 1 (fun ij -> 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[,]

[B]回答:[/B]

你不要ref yi ,你想要

let mutable yi = new obj() thatfunc(..., &yi, ...) 尽管我认为仅靠这一点是无法解决的。是否有C#示例调用此特定API?



[url=https://stackoverflow.com/questions/2824675]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 01:09

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.