poster
2019-12-10, 20:41
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.) 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]更新资料
这是更正的版本:
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?
更多&回答... (https://stackoverflow.com/questions/2824675)
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.) 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]更新资料
这是更正的版本:
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?
更多&回答... (https://stackoverflow.com/questions/2824675)