using matlab functions in c

11
Using Matlab functions in C# (2009 a) 13 Jul 8 Votes 1. Create a m-file function which encapsulate your subroutine, and save it to a folder: 2. Add this folder to Matlab path: in Matlab, File > SetPath

Upload: dewi-amore

Post on 26-Oct-2014

168 views

Category:

Documents


9 download

TRANSCRIPT

Page 1: Using Matlab Functions in C

Using Matlab functions in C# (2009 a)13 Jul       8 Votes

1. Create a m-file function which encapsulate your subroutine, and save it to a folder:

2. Add this folder to Matlab path: in Matlab, File > SetPath

Page 2: Using Matlab Functions in C

3. In MATLAB command window, type “deploytool”

4. In MATLAB window, click the File new icon to create a New Deployment Project.

In the New Deployment Project dialog box, select MATLAB Builder NE and .NET Component.

In the name textbox, type whatever meaningful titles, and then click OK.

Page 3: Using Matlab Functions in C

5. Select “Generate Verbose Output” and Add the source *.m file to the project, and then save the project.

6. Build the component by clicking the “Build “button in the Deployment Tool toolbar. The build process begins, and a log of the build appears in the Output pane of the Deployment Tool. The files that are needed for the component are copied to two newly

Page 4: Using Matlab Functions in C

created subdirectories, src and distrib. A copy of the build log is placed in the src directory. The building process may take quite a few seconds…, have a cup of coffee now…

Now the output folders and files may like this:

7. Now, almost done! Let us try and test it in VisualStudio.net, write source code for a C# application that accesses the component, for instance:

NDimVoronoiDiagram vg=new NDimVoronoiDiagram ( );

Page 5: Using Matlab Functions in C

MWArray [] Results=new MWArray[2];Results = (MWArray [])vg.CalVoronoiDiagram(2, (MWNumericArray)X, (MWNumericArray)Y);

double [,] VoronoiVertices =(double [,]) Results[0].ToArray();object[,] VoronoiEdgeIndice = (object[,])Results[1].ToArray();8. Add two references, \toolbox\dotnetbuilder\bin\win64\v2.0\MWArray.dll and the generated dll, in our case, NDimVoronoiDiagram.dll, build the solution, and that is it.

Note that a ” Runtime Error R6034, An application has made an attempt to load … ” occurs one when initializing the NDimVoronoiDiagram object, and I am running Matlab on Vista x64, and I cannot figure out the reason, and if any one has solutions, please let me know.

*****************************************

Extra note I: I put the matlab project inside my VS project folder, and compile it, it results in the failure of reflecting the output dll using ObjectBrowser in VS, and this directly leads to unknown namespace which contains the Matlab class.Very weird.

Extra note II: The namespace, class name and the method name follows the rules as dipicted in the figure below.

Page 6: Using Matlab Functions in C

Using Matlab functions in C# (2009b)12 Nov       1 Votes

Matlab has changed a bit of the command “deploytool” in 2009a and 2009b. To see how to use matlab functions in 2009a, see my previous post here.

In 2009b, this has changed a bit and this post will show you how to accomplish the same task in Matlab 2009b.

1. Start Matlab and key in “DeployTools”, Select “.NET assembly”, and the project location and name

Page 7: Using Matlab Functions in C

 

2. After clicking the OK button…

 

3. In the “Build” Tab, Add a class which corresponds to the .Net Class Name, and matlab .m files, each .m file (i.e. a matlab function) corresponds to a method of the .Net Class.

 

4. click the Build button.

Page 8: Using Matlab Functions in C

Particular note that your project folder structure should be supposedly SIMPLE, and better avoid spaces or less frequently used chars, otherwize, it is very likely to fail in the build process.

5. Now start a C# Winform project, add a reference to the output built by Matlab

  In the object brower, you can see that the class “VoronoiCell” appears, and it has two important functions, as expected, with different overloadded prototypes!

Page 9: Using Matlab Functions in C

What a cool feature!

6. Now, add another assembly called “Mwarray.dll”, located in \toolbox\dotnetbuilder\bin\win64\v2.0 folder.

7. Create a C# project, and test with some code, for instance:

1: MyVoronoiCell vgc= new MyVoronoiCell(); 2: double[] X = { 0.5, 0, -0.5, -0.2, -0.1, 0.1, 0.1 }; 3: double[] Y = { 0, 0.5, -0.5, -0.1, 0.1, -0.1, 0.1 }; 4: MWArray[] Results = (MWArray[])vgc.GetVoronoiEdge 5: (2, 6: MWNumericArray)X, 7: MWNumericArray)Y); 8: double[,] EdgeX = (double[,])Results[0].ToArray(); 9: double[,] EdgeY = (double[,])Results[1].ToArray();

8. Now, problem comes. If you run the program, exception will be thrown at Line 1, and there are a few possible cases

1. {"Could not load file or assembly ‘MWArray, Version=2.9.1.0, Culture=neutral, PublicKeyToken=e1d84a0da19db86f’ or one of its dependencies. An attempt was made to load a program with an incorrect format."}

This is mostly due to the absense of MWArray.DLL in GAC, and the simplest solution is to add it to GAC by typing

gacutil /i MWarray.dll

2. {"The type initializer for ‘MathWorks.MATLAB.NET.Utility.MWMCR’ threw an exception."}, {"Trouble initializing libraries required by Builder NE.\n"}, "   at MathWorks.MATLAB.NET.Utility.MWMCR..cctor()", or {"Trouble initializing libraries required by Builder NE.\n"}  or

{Unable to load DLL ‘mclmcrrtXXX.dll’} or

Page 10: Using Matlab Functions in C

MathWorks.MATLAB.NET.Utility.MWMCR.mclmcrInitialize(); at MathWorks.MATLAB.NET.Utility.MWMCR..cctor()

For these kind of applications in x86 platform, the solution is to run MCRInstaller.exe to install the Matlab Compiler Runtime, it should work once MCR is installed. Restart OS after MCRInstaller.exe is installed!!! Restart VisualStudio, Matlab!!!

Now rebuild the project, all is fine.