Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
回复
 
主题工具 显示模式
旧 2019-12-10, 20:41   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 从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输出参数?

更新资料

这是更正的版本:

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[,]

回答:

你不要ref yi ,你想要

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



更多&回答...
poster 当前离线   回复时引用此帖
回复


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 05:17


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