Call "log" functions from c# through COM failed with exceptions.

3 次查看(过去 30 天)
I am simply testing executing matlab functions through C# by the following,
var matlab = new MLApp.MLApp();
var n = new int[10000000];
var r = new Random();
for (int i = 0; i < 10000000; ++i)
n[i] = r.Next(1, 1000000);
var t = DateTime.Now;
for (int i = 0; i < 10000000; ++i)
{
object result = null;
matlab.Feval("log2", 1, out result, n[i]);
}
Console.WriteLine((DateTime.Now - t).TotalSeconds);
This fails with exceptions: Exception thrown: 'System.Reflection.TargetInvocationException' in mscorlib.dll Exception thrown: 'System.Runtime.InteropServices.COMException' in mscorlib.dll
However, if you test another function "randsample", it will work.
var matlab = new MLApp.MLApp();
var n = 1000;
var t = DateTime.Now;
for (int k = 0; k < 1000; ++k)
{
object result = null;
matlab.Feval("randsample", 1, out result, n, k);
}
Console.WriteLine((DateTime.Now - t).TotalSeconds);
Both are buidin math functions. I don't know why one works but the other does not. Any help is appreciated! Thanks!

回答(1 个)

Harsh
Harsh 2017-5-17
Make sure the correct reference to mlapp.tlb is added in Visual Studio or any other IDE you are using. Also make sure that MATLAB is registered as a COM server. You can read more here:
Additionally make sure you are passing the correct number of input and output arguments.
The following resources may also be useful:
If the issue persists, please contact MathWorks Technical Support here: https://www.mathworks.com/support/contact_us/
Please be sure to provide a detailed description of the issue and attach any relevant files / code / examples required to investigate the issue.

类别

Help CenterFile Exchange 中查找有关 MATLAB Compiler SDK 的更多信息

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by