Trying to launch a MATLAB plot program from a .NET C# windows application

5 次查看(过去 30 天)
Hi, I am trying to call a MATLAB .m file from a C# windows app. I was able to create a .NET assembly using the MATLAB compiler. I added a reference to the dll created by the compiler and had to change the visual studio application properties, platform target to x64 to get the C# app to build ok. I also added the MWArray dll as a reference, not sure if required. It looked like the compiler created 2 dlls, one with the Native keyword added. I included the other dll as well as the MWArray dll as references.
Anyhow I have something like using namespace; also have using Mathworks.NET; I have mlclass mymatlabclass=new new mlclass ();//mlclass is the class name of the matlab assembly. mymatlabclass.method(); the program should launch the MATLAB plot with the second line but just seems to hang running from the debugger in Visual Studio with no errors. I was able to instantiate the class ok.
  5 个评论
Paul
Paul 2018-10-3
Got it to build the package, just added a function at the top of the file. I should be able to post some code soon, thanks!
Paul
Paul 2018-10-3
Still missing something I have using Visual; using Mathworks.MATLAB.NET; I added Visual.dll and MwArray.dll as references to the .net project. Visual is the name of the namespace. I have a button on a windows form and in button select event I have Myclass callMat = new MyClass();//this causes an exception then I tried Myclass callMat = null; and callMat.NameofMethod(); but get null error when calling method, callMat is a null object. The compiler also created a dll called VisualNative.dll, was not sure if I needed to include it with the .net project. Thanks Paul.

请先登录,再进行评论。

回答(2 个)

Paul
Paul 2018-10-1
Hi I should be able to provide some sample code within the next few days. Thanks Paul.

Kojiro Saito
Kojiro Saito 2018-10-3
Suppose you have compiled MATLAB function (call "NameofMethod") to .NET assembly (call Visual) and got Visual.dll. I think you need to enclose your codes with "try catch" because the class instantiation and method invocation would make their exceptions at run time.
using Visual;
using Mathworks.MATLAB.NET;
namespace YOURNAMESPACE
{
class Program
{
static void Main(string[] args)
{
Myclass callMat = null;
int numOfOutput = 0;
try
{
callMat = new MyClass();
callMat.NameofMethod(numOfOutput);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
}
}
About **native.dll, here is a document. It uses Native .NET API instead of MWArray API.
  7 个评论
Kojiro Saito
Kojiro Saito 2018-10-10
I'm not sure good way of debugging other than Visual Studio debugger, but there's a document of calling compiled MATLAB plot from C#. Please see this document (in Simple Plot section). It might be helpful.
Paul
Paul 2018-10-11
Ok, thanks I found a permissions error in the windows event viewer but can not tell if it is the issue.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Deploy to .NET Applications Using MWArray API 的更多信息

标签

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by