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

3 views (last 30 days)
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!

Answers (1)

Harsh
Harsh on 17 May 2017
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.

Categories

Find more on Write COM Applications to Work with MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!