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

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 Commenti

Could you share some sample codes for our investigating in detail?
Hi I ran into an issue just trying to create the Mat lab executable using the compiler. I was able to select the net assembly and files required for application to run. The package button is not enabled and I get the error application must have a target and entry point defined. I did not see an add main file option, possibly I am using the wrong compiler option? Thanks Paul.
Just some additional information, When I select the add exported function (maybe you select this to select the main file) I get the error only matlab functions with a single entry point are acceptable as exported functions. Guessing I need to modify the matlab .m file?
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!
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.

Accedi per commentare.

Risposte (2)

Hi I should be able to provide some sample code within the next few days. Thanks Paul.
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 Commenti

Hi thanks for the information. I tried it but when it hits callMat= new MyClass(); it jumps to the catch code with an exception error so never executes the callMat.MethodName(NumOfOutPut); It is almost like it can not find MyClass() which is called Visual. I did reference the Visual.dll created with the Mat Lab compiler and do have using Visual; Any other ideas?
Hi, just I was able to get it to make it past callMat=new MyClass(); but it hangs now on the method call.
I was able to get it to instantiate the class by going to build->configuration manager and changing the setting any CPU to x64.
That is a good news. Yes, we need to change platform target from any to x64 as the step 4 of this document describes. It's because MATLAB (& MATLAB Runtime ) supports only 64 bit.
Ok thanks for the additional information. Any idea why the Mat plot does not launch and the Visual Studio project just hangs up on the method call? I am not sure if there is any way to debug this, thanks Paul.
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.
Ok, thanks I found a permissions error in the windows event viewer but can not tell if it is the issue.

Accedi per commentare.

Prodotti

Release

R2017a

Richiesto:

il 20 Set 2018

Commentato:

il 11 Ott 2018

Community Treasure Hunt

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

Start Hunting!

Translated by