RSS

Using Matlab functions in C# (2009 a)

13 Jul

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

WriteMFunction.png

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

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.

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 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 ( );

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.

Note1.png

About these ads
 
23 Comments

Posted by on July 13, 2009 in Dotnet/C#, Maths

 

23 responses to “Using Matlab functions in C# (2009 a)

  1. aminD

    February 17, 2010 at 5:39 pm

    ThanX alot !

     
  2. Majd

    April 11, 2010 at 5:13 am

    Thanks very much , this is the most useful tutorial on the net.

     
  3. ف wildchild ۞

    April 20, 2010 at 4:57 pm

    that was the common error if you are using Matlab 2009a. i heard the later version fix this error. i too have similiar error using 2009a. now, i’m using 2007a and its working just fine :)

     
  4. Iman

    April 27, 2010 at 6:28 pm

    That’s cool,really good and useful tutorial.

     
  5. Aidan

    June 17, 2010 at 10:37 pm

    How did you get the image of the plot to show in the program window? The code for that would be very useful!

     
  6. xinyustudio

    June 18, 2010 at 12:33 am

    You can try MS Chart control, which is shipped for free in .Net 4.0 library. The control which I use in the post is called ZedGraph control. Google it and get it. Good luck and happy coding!

     
  7. Aris

    July 9, 2010 at 10:51 pm

    What about if your .m file functions calling sub-functions. Do these subfunctions needed to be added as .Net components as well?

     
  8. xinyustudio

    July 9, 2010 at 11:20 pm

    Interesting question, shall look into that.

     
  9. Mehdi

    September 20, 2010 at 7:37 pm

    Hi
    I wrote this code in my m file and I made the dll. But it didn’t work. Is some thing wrong in my code? Is it necessary to use “Function”?

    my code:
    cd(‘c:\S_function_final’)
    open untitled22.mdl
    [t,x,y] = sim(‘untitled22′)

     
  10. xinyustudio

    September 20, 2010 at 8:00 pm

    Yes, the m file should be declared as functions, even though it contains zero argument. More details can be found in Matlab help.

     
  11. Mehdi

    September 21, 2010 at 9:03 pm

    Thanks for your answer.
    I changed the m file code to this (my m file name is A3):
    function open
    cd(‘c:\S_function_final’)
    open untitled22.mdl
    [t,x,y] = sim(‘untitled22′)
    end

    and in my .net project I wrote this:
    Dim o As New A3new.A3newclass
    o.A3()

    but it didn’t work and this is the error:
    mwmcr::evaluatefunction error: error using ==> open at 127 file ‘Untitled22.mdl’ not found. Error in A3 at 2

     
  12. xinyustudio

    September 22, 2010 at 9:44 am

    Mehdi, you are right this might not work, and the function should be like below:

    SomeAction.m
    ==================================
    function SomeAction() % You might need (), even though no arguments
    %Put your code here
    end
    ==================================

    The discussion is not strongly related to the topic in this post, this is more a Matlab related issue, and less relevant to the Matlab+.Net. Anyway, hope it is useful to you.

     
  13. Darko

    November 23, 2010 at 7:25 am

    I made .dll from my .m file, but now I have problem how to take my outputs from the function. My function have 2 outputs. If someone know how to use the value from outputs in my C# code please help me.

     
  14. xinyustudio

    November 23, 2010 at 8:22 pm

    Suppose your function in Matlab is like this:
    [A, B]=GetSomeOutput(C)

    —————–C#—————–
    MWArray[] Output=GetSomeOutput(C);
    A=Output[0];
    B=Output[1];
    —————–C#—————–

    You can view the signature of the compiled functions (DLL) using “Object Browser” in Visual Studio.

    Is this what you want?

     
  15. Abd

    October 29, 2011 at 11:07 am

    I have tried to add WMArray as namespace ,in C# , but allways i got error says that MWArray is could not be found , and also The gerenerated DLL , which I made it , I could not use it ..is a TESTDLL and magicsqurearray inside it ,, any help please

     
  16. ali kipel

    December 22, 2011 at 4:42 pm

    I thing Abd is Right. What the MWArray [] Results=new MWArray[2]; I didnt find reference about MWArray how can get the variable values from matlab. I mean I use ANS in matlab how can . Please say somethings thank you..

     
  17. ali kipel

    December 22, 2011 at 4:43 pm

    And Can you send me some example about this thank you

     
  18. Armin

    January 25, 2012 at 12:31 pm

    thankss alot , very very nice,

     
  19. Skyns

    March 22, 2012 at 2:56 pm

    Please, help me to solve the problem:
    my mablab function:
    ——————————
    function [y]=xmin(x)
    [b, a]=butter(6,0.2);
    [y]=filtfilt(b, a, x);
    ——————————
    C#:
    ——————————-
    const int X = 150;
    MWArray[] In = new MWArray[X];
    MWArray[] Out = new MWArray[X];
    filtfilt ff = new filtfilt();
    for (int i = 0; i < X; i++)
    if (i < 50)
    In[i] = 0;
    else
    if (i < 100)
    In[i] = 1;
    else
    In[i] = 0;
    ff.xmin(1, ref Out, In); //accessing matlab function(here appeares acception!)
    ——————————-
    After building C# project I get acception:
    ————————————————–
    An unhandled exception of type 'System.Exception' occurred in MWArray.dll
    Additional information: … MWMCR::EvaluateFunction error …
    Error using xmin
    Too many input arguments..
    —————————————————-
    HOW CAN I PUT AN ARRAY FOR INPUT INTO THE FUNCTION: ff.xmin(1, ref Out, In); ???
    please, help, It is very important.

     
  20. Monim Sudan

    March 25, 2013 at 3:09 pm

    Thanks and Regards
    Nice test

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 45 other followers

%d bloggers like this: