RSS

Using Matlab functions in C# (2009b)

12 Nov

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

     M2009b_A

  2. After clicking the OK button…

    M2009b_B 
    M2009b_C

  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.

     M2009b_D

  4. click the Build button.

    M2009b_E M2009b_F
    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

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

    M2009b_H M2009b_I
    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

      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.

About these ads
 
63 Comments

Posted by on November 12, 2009 in Dotnet/C#, Maths

 

63 responses to “Using Matlab functions in C# (2009b)

  1. marko

    March 18, 2010 at 7:41 pm

    Error 1 The type or namespace name ‘MWArray’ could not be found (are you missing a using directive or an assembly reference?) C:\Users\Ultimate\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 9 7 WindowsFormsApplication1

     
  2. xinyustudio

    March 18, 2010 at 9:25 pm

    Search MWArray.dll in your hardrive, and then add thie reference to your project. Most possible folder is ‘MatlabInstallDir\toolbox\dotnetbuilder\bin\win64\v2.0′ or ‘MatlabInstallDir\toolbox\dotnetbuilder\bin\win32\v2.0′.
    Cheers!

     
  3. Jack

    May 25, 2010 at 4:33 am

    Would it be possible to post a link where we can download all the actual files involved (.m and .sln .cs)? Also for the R2009a version? Thanks

     
  4. xinyustudio

    May 26, 2010 at 4:50 am

    Quite trivial if you have read this post, DIY!

     
  5. xinyustudio

    May 29, 2010 at 7:17 am

    If you encounter the following exception:

    “The type initializer for ‘xxxxxxx’ threw an exception. —> System.BadImageFormatException: Could not load file or assembly ‘MWArray, Version=2.10.0.0, Culture=neutral, PublicKeyToken=e1d84a0da19db86f’ or one of its dependencies. An attempt was made to load a program with an incorrect format.

    You should check if your C# program is configured in x86 or x64 configuration. In VisualStudio 2010, when you start programming, the default mode is x86 configuration, even though your windows OS is 64 bit. After changing the configuration to x64 in VS2010, the exception should disappear now.

    Enjoy.

     
  6. Christos75

    June 19, 2010 at 7:11 pm

    In this case: “You should check if your C# program is configured in x86 or x64 configuration. In VisualStudio 2010, when you start programming, the default mode is x86 configuration, even though your windows OS is 64 bit. After changing the configuration to x64 in VS2010, the exception should disappear now.” Ok the exception disappeard but a new exception appear… TypeInitializationException was unhandled and the connectivity Visual Studio 2010(My C# program) with Matlab R2010a failed!!!!
    Do you have any idea on how to deal with this???
    Thanx in advence!

     
  7. Christos75

    June 19, 2010 at 7:31 pm

    This exception-TypeInitializationException-could happens because IntelliTrace debugging is available only for x86 applications,not for x64…
    If you want i could post the code here. The same code works perfect in Visual Studio 2008 and Matlab R2009b,but in VS2010 and Matlab R2010a doesn’t…
    This is my problem in the last 5 days…any solution could be appreciated!!!
    Thank you for your time in advance!

     
  8. xinyustudio

    June 21, 2010 at 11:09 pm

    Chirstos75,

    I cannot reproduce the exception, and you need give more info in order to troubleshoot this.

     
  9. bleonardo

    August 27, 2010 at 2:02 am

    Did you notice any memory leak using this code? Every code I tried to run (even the matlab examples) don’t free the allocated memory at all… Even when I call Dispose(), the memory is not released… Do you have the same problem?

     
  10. xinyustudio

    August 27, 2010 at 2:57 am

    how do you know the memory leak is due to Matlab? I am interested to know the details. Thanks.

     
  11. bleonardo

    August 27, 2010 at 5:13 am

    I’m using a memory profiler in a program that just instantiates a class (from matlab) or a MWArray and then disposes everything… The profiler show a large memory increase and no release at all… And I’m not even calling any method from the class… The profiler shows some event handlers from MathWorks.MATLAB.NET.Utility that could be causing this problem, but I could not find any way to access this events…

    I guess it’s something that start to happen from MCR 7.11… I was just wandering if anyone had the same problem…

     
  12. xinyustudio

    August 28, 2010 at 5:04 am

    Bleonardo,

    This is a very interesting comment, and I agree that Matlab DotNet handshakeing still has some technical problems. In addition to the meomory leak problem as you have pointed out, I found some functions are still unavailable, for instance, if your *.m file contains some LAPACK algorithm, when you compile the source into the DLL assembly and call it in DotNet, you might encounter an exception about the “mllapack.dll”. Matlab ‘s answer to this is that this is a SSE2 problem, see here for details. But I am pretty sure that my PC supports SSE2 instruction sets.

    I would expect Matlab can fix these problems in the future update, and should you have any solutions, please let me know. Thanks.

     
  13. Suchmaschine

    October 27, 2010 at 3:11 pm

    This is a really good read for me, Must admit that you are one of the best bloggers I ever saw.Thanks for posting this informative article.

     
  14. Lakshmi Prasad

    December 24, 2010 at 5:58 pm

    Hi I receive the following error:
    Trouble initializing libraries required by Builder NE.

    The type initializer for ‘MathWorks.MATLAB.NET.Utility.MWMCR’ threw an exception.
    The type initializer for ‘MathWorks.MATLAB.NET.Arrays.MWArray’ threw an exception.

    Stack Trace :
    System.TypeInitializationException: The type initializer for ‘MathWorks.MATLAB.NET.Arrays.MWArray’ threw an exception. —> System.TypeInitializationException: The type initializer for ‘MathWorks.MATLAB.NET.Utility.MWMCR’ threw an exception. —> System.Exception: Trouble initializing libraries required by Builder NE.

    at MathWorks.MATLAB.NET.Utility.MWMCR..cctor()
    — End of inner exception stack trace —
    at MathWorks.MATLAB.NET.Utility.MWMCR.SetResourceManager(ResourceManager resourceManager)
    at MathWorks.MATLAB.NET.Arrays.MWArray..cctor()
    — End of inner exception stack trace —
    at MathWorks.MATLAB.NET.Arrays.MWArray.op_Implicit(Double scalar)

    I use 64 bit Windows 2008 R2..How do i resolve this, i have tried the above steps, but not able to overcome this issue.

     
  15. xinyustudio

    December 24, 2010 at 7:34 pm

    Did you set Visual Studio in x64 configuration?

     
  16. Lakshmi Prasad

    December 29, 2010 at 1:01 pm

    Yes, I use x64 Configuration..

     
  17. xinyustudio

    December 29, 2010 at 7:08 pm

    From the comments, I cannot figure out the reason, you might contact Matlab support to troubleshoot this?

     
  18. Lakshmi Prasad

    January 4, 2011 at 10:25 pm

    Hi Christos 75 / XinyStudio,

    Is there a way to overcome the System.TypeInitializationException exception in Matlab 2010a in 64 bit?

    Regards,
    Lakshmi Prasad

     
  19. sara

    January 6, 2011 at 5:04 am

    hi,
    how to use matlab neural network in c#?
    tank you

     
  20. xinyustudio

    January 7, 2011 at 12:12 pm

    Lakshmi,

    There is nothing special to tackle the System.TypeInitializationException exception in Matlab 2010a and 2009 a/b in x64 setting.The same solution applies. If you encounter weird problems, I suppose that it is most likely related to your OS problems. Try to get it done on a clean OS or virtual OS (e.g. Virtual Box or VMWare), and see if the same problems pop up.

    Hope this helps.

     
  21. xinyustudio

    January 7, 2011 at 12:14 pm

    Sara,

    The use of particular functions(e.g. neural network) is nothing special to use a simple matlab function, you just wrap it in the function, compile and run! That is it!

     
  22. Lakshmi Prasad

    January 19, 2011 at 9:01 pm

    Hi,

    In the same system, i tried creating Test console application and WinForm application and tried instantiating the MWArray and MWNumeicArray and these go fine without any issue.

    Basically i ported my original application from 32 bit and tried the same using this test application. Everything goes fine in my test app. and the Project properties are also same when i compare both these…
    I have not tried this using in a Clean PC.Let me do that.

    Thanks,
    Lakshmi Prasad

     
  23. James

    August 31, 2011 at 6:37 pm

    I meet the problem of “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.”, and I have tried the method that you have mentioned, but it doesn’t work. It is a 32-bit machine and a Matlab2010a was set up actually. Would you please give me some advice?

     
  24. xinyustudio

    August 31, 2011 at 10:11 pm

    James, I need more details to help out.
    What functions have you encapsulated? Did you install x86 version of Matlab?

     
  25. xinyustudio

    August 31, 2011 at 10:12 pm

    James,

    Search with the keyword “matlab” in this blog, there are more than one blog entries about programming Matlab with .Net.

    Cheers!

     
  26. hothero

    September 18, 2011 at 9:29 pm

    useful article. thanks a lot for this!!!

     
  27. thomas

    November 9, 2011 at 3:18 am

    Please if you work for Mathworks release a version of MWArray which Target .NET framework 4.0. There should be a version for all .NET framework. Without this it is not posisble to run .NET 4.0 client calling into matlab generated asembly on Windows 7 64 bit simply because .NET framework 2.0 is not supported. When I target 4.0 inside matlab I expect ALL dependencies to be .NET Framework 4.0 !!!

    Thanks

     
  28. boris

    November 23, 2011 at 9:28 pm

    Hi

    I want to use some matlab functions in C#, i do all the steps above but every time that i run the program I obtain in this line code ” double [,] h=(double[,])Results.ToArray(); ” the next error:

    Could not load file or assembly ‘ManagedCPPAPI.netmodule’ or one of its dependencies. Error en una rutina de inicialización de biblioteca de vínculos dinámicos (DLL). (Exception from HRESULT: 0x8007045A)

    How do i resolve this.

     
  29. xinyustudio

    November 24, 2011 at 4:59 pm

    Boris, we need more details to help out. Your current info is too limited to diagnose.

     
  30. boris

    November 24, 2011 at 9:51 pm

    Hi

    I create a function in Matlab 2009a:
    J=seg(I,hs,hr,tol)
    Inputs:
    I: is a matrix
    hs , hr, tol: are parameters
    Outputs:
    J:is a matrix

    I follow the steps in your other post “Using Matlab functions in C# (2009 a)” to create the .dll

    Im workin in Visual Studio 2010. In my application in a specific button I put the following code:
    1-MWArray [] Results=new MWArray[1];
    2-Segmentation image= new segmentation();
    3-Results[0] = (MWArray[])image.seg(1,I,hs,hr,tol);

    At the beginning I was having in the line 3 the next problem:

    “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.”

    But I use the solution that you have here by adding gacutil /i MWarray.dll and now my new problem in the line 3 is:

    “Could not load file or assembly ‘ManagedCPPAPI.netmodule’ or one of its dependencies. Error en una rutina de inicialización de biblioteca de vínculos dinámicos (DLL). (Exception from HRESULT: 0x8007045A)”

    How do i resolve this.

     
  31. xinyustudio

    November 25, 2011 at 9:01 pm

    I suspect that some .net DLL is missing or could not be found by MWMCR. You might try search “ManagedCPPAPI.netmodule” to see where this file is located. If you are using x86 architecture, you can find it in the “bin\debug” folder of your VS project folder. If you are using in a x64 mode, you probably will not see it, at least, this is the case to my tests.

    To recap, you need to make sure the file “ManagedCPPAPI.netmodule” is put in the same folder where you binary exe is located.

    Good luck and happy coding!

     
  32. boris

    November 27, 2011 at 9:47 pm

    Ok i will try to do this. Thanks.

     
  33. boris

    December 2, 2011 at 9:58 pm

    Hi
    I finish my program. How can I use it in other pc that not has installed Matlab?

     
  34. xinyustudio

    December 2, 2011 at 10:07 pm

    You should install the MCRInstaller.exe on the target machine, and then copy the dll and related exe, preferably into the same folder; or otherwise, make sure the related dll path are appended to the environment variable “path”. To recap, you need to make the binary exe know where to find the dependent dlls. Good luck.

    BTW: Boris, can you please share how you have troubleshot the problem above?

     
  35. boris

    December 2, 2011 at 10:15 pm

    In fact I dont know where was the problem when I used Matlab 2009a, I install the Matlab 2010b and then follow all the steps above explained and works fine.

     
  36. sara

    February 19, 2012 at 5:34 am

    which matlab version support this ? boris , u said that 2009a didnt work

     
  37. xinyustudio

    February 19, 2012 at 12:41 pm

    Matlab 2009a, b, 2010 a & b all work fine. Boris may miss sth in his implementation?

     
  38. Héctor

    February 22, 2012 at 5:24 pm

    thanks you so much xinystudio, i’ve get to run simple functions of matlab in Visual Studio 2010 and plot it, now im searching for the component ZedGraph for paint my graph in the window app.
    Cheers!!

     
  39. swapna

    March 28, 2012 at 5:44 pm

    Hi xinyustudio,
    I have deployed my application on client PC.
    I am also getting the error as
    “.{“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”}”
    But, I have installed MCR 7.11 properly and after that the PC is restarted. But, still this error is there. Can you please help urgently ???

     
  40. xinyustudio

    March 28, 2012 at 5:48 pm

    swapna, the info is far less enough for me to help. More info is expected to troubleshoot this, e.g. what os, what version of VS, x86 or x64 etc.

     
  41. boris

    April 19, 2012 at 2:05 am

    Hi I find how to fix in other way my problem add this configuration to the app.config file. this should figure out the problem. I think that it is related to the version of the dll that was created with matlab

    Regards

     
  42. xinyustudio

    April 19, 2012 at 9:29 am

    Brilliant, Boris. Can you send me your app.config and I can attach it here so all can share it ?
    If possible, please send it to XinyuStudio@gmail.com, thanks!

     
  43. Maryam

    July 6, 2012 at 12:00 am

    Hi, I have the same problem as “Christos75″ is describing in this page. I`d really appreciate any help.
    I get an ‘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.” when I try to connect my matlab .Net assembly to C#. I change the configuration to x64. The error resolves. However, I get another error indication that one of the previous libraries developed initially in C# has the same problem: “‘DAL, Version=1.2.1.0, Culture=neutral, PublicKeyToken=…’ or one of its dependencies. An attempt was made to load a program with an incorrect format.”

    Can you help me how to resolve this?

     
  44. xinyustudio

    July 6, 2012 at 9:13 am

    Maryam, I think the reason might be: the DAL assembly you use is compiled in x86 configuration, so when you switch the project setting to x64, the DAL is not compatible with this setting any longer. One possible solution is to recompile your DAL solution in x64 mode, and reference the x64 version in your solution; of if you don’t have the source, you might have to try your program on a x86 (32 bit) OS. Happy coding and hope this helps!

     
  45. Maryam

    July 6, 2012 at 11:02 pm

    Many Thanks; That problem resolved. Actually I tried to create my Matlab MCR dll libraries with 32 bit matlab (on a 64 OS though). However, now I get another error. When I apply my .net libraries created in deploytool in a new project, everything`s fine and I have my expected outputs; but when I merge it into the main application solution, add them as references, and apply them for coding, then I receive this exception: “System.TypeInitializationException was unhandled Message: The type initializer for ‘com.SBD.SBDclass’ threw an exception.” SBDclass is the type that has been created with deploytool (Misrosoft Framework 3.5). Do you have any idea about this?
    I even tried creating a new project and referring to it in the main application solution; but I received the same error!

     
  46. xinyustudio

    July 10, 2012 at 5:56 pm

    As I don’t know where SBDclass is from, I cannot help. From your description, I am pretty sure that the problem is due to mixed config of x86/x64 config in your referenced assemblies. Given this hint, it is trivial to troubleshoot your problems.

     
  47. Divya

    September 7, 2012 at 4:35 pm

    Hi,
    I have deployed a Matlab .NET package and wrote a sample application in VS 2010 (64-bit). It builds successfully. But while running it throws an exception from

    at MathWorks.MATLAB.NET.Arrays.MWNumericArray.validateInput(Array realData, Array imaginaryData)
    at MathWorks.MATLAB.NET.Arrays.MWNumericArray.FastBuildNumericArray(Array realData, Array imaginaryData, Boolean makeDouble, Boolean rowMajorData)
    at MathWorks.MATLAB.NET.Arrays.MWNumericArray..ctor(Array realData, Array imaginaryData, Boolean makeDouble, Boolean rowMajorData)
    at MathWorks.MATLAB.NET.Arrays.MWNumericArray.op_Implicit(Array realData)
    at TestIntegration.Form1.buttonMagicSquare_Click(Object sender, EventArgs e) in K:\TestIntegration\TestIntegration\Form1.cs:line 46
    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
    ……”

    The code that causes the exception is

    private void buttonMagicSquare_Click(object sender, EventArgs e)
    {
    MWNumericArray input = null;
    MWNumericArray output = null;
    int inputarg = 1;

    MLTestClass obj = new MLTestClass();
    input = new MWNumericArray(5);

    output = obj.makesquare(1,input); /// Error comes from the second argument in this stmt,

    output = (MWNumericArray)output[0];

    MessageBox.Show(output.NumericType.ToString());

    }
    Can you help me in solving this problem?

    Thanks,
    Divya

     
  48. Divya

    September 13, 2012 at 1:02 pm

    The problem posted in the above comment has been solved. I think it was due to some incompatible settings parameter.
    Divya

     
  49. Moustafa

    November 8, 2012 at 8:55 am

    Thanks a lot .. This article was really a live saver !

     
  50. Jungwoo

    June 18, 2013 at 8:40 am

    Hello, I’m searching my solution for problem. and than i find this site and you. I’coding C# with matlab compiler NE Tool. but My code has the error about mkl.dll. but my code is complete building and no error. i don’t know this situation. Details, when debugging and connecting to matlab functions in added class to making code, generate pop up windows about mkl.dll. and this pop up present “Can’t create the specifed module.” I’m so so sad… I’m crying soon. please help my problem. – See more at: http://www.geospecialling.com/index.php/2013/01/a-net-based-user-interface-for-matlab-applications/#sthash.riyaPhNm.dpuf

     
  51. sam

    September 26, 2013 at 5:58 pm

    Hi, thanks for the very interesting post. I am having trouble getting this to work in Azure.. Any ideas:

    I have compiled a MATLAB function using R2013a into a .NET dll using Builder NE.

    I can reference the dll and call the function successfully from a C# Console app.

    But the same code fails when running in a worker role through an Azure Cloud Service project. The application just crashes out silently and stops debugging at the line that initializes the class in the dll. There is this message in the Debug Output window:

    “The program ‘[8620] WaWorkerHost.exe: Managed (v4.0.30319)’ has exited with code -529697949 (0xe06d7363) ‘Microsoft C++ Exception’.”

    I have tried a couple of things in the project properties: Set the Platform target to x64; Unchecked ‘Enable the Visual Studio hosting process”

    It fails both running in the Azure Emulator on my dev machine, and when deployed to an Azure worker role on a Cloud Service. Yet when I execute a console app that calls the same dll via Process.Start it calls the MATLAB/MCR/.Net dll successfully. (I have a startup task that silently installs the MATLAB MCR)

    Thanks

     
  52. xinyustudio

    September 26, 2013 at 7:22 pm

    Sam, I have not tested this in cloud apps. What exception info does it give you?

     
  53. sam

    September 26, 2013 at 8:00 pm

    There’s no exception other than that line in the Debug Output window.

    It works in a console app, but not in the Azure emulator or in Azure proper. Ah well.. just thought I’d ask as your post was really good and hoped you might have tried it!

    Thanks

     
  54. Andreia

    January 20, 2014 at 2:53 am

    Hi xinyustudio,

    I have compiled a MATLAB function using R2012b into a .NET dll using Builder NE.

    I can reference the dll and call the function successfully from a C# Console app.
    I already have include this namespace: MathWorks.MATLAB.NET.Arrays

    I can build too without errors (Visual Studio 2013), just with one warning
    “There was a mismatch between the processor architecture of the project being built “MSIL” and the processor architecture of the reference “MWArray, Version=2.12.1.0, Culture=neutral, PublicKeyToken=e1d84a0da19db86f, processorArchitecture=AMD64″, “AMD64″. This mismatch may cause runtime failures. Please consider changing the targeted processor architecture of your project through the Configuration Manager so as to align the processor architectures between your project and references, or take a dependency on references with a processor architecture that matches the targeted processor architecture of your project. MatLabTest”.

    But when i run my application test, it fails in this line:
    “MyFingerClass obj = new MyFringerClass()”
    the error is: “TypeInitializationException was unhandled”

    I follow yours steps and the steps from the site “http://domoreinlesstime.wordpress.com/2013/01/26/access-matlab-from-c/comment-page-1/#comment-8″, and also this one “http://internetexception.com/post/2013/01/20/Using-with-Matlab-from-C-sharp-application.aspx” to communicte with matlab.

    Can you solve it?

     
  55. Andreia

    January 20, 2014 at 3:30 am

    I resolved the warning, i put everything in x64.
    But i have another error….

    When i execute the command:

    var activationContext = Type.GetTypeFromProgID(“matlab.application.single”);
    var matlab = (MLApp.MLApp)Activator.CreateInstance(activationContext);

    MyfingerClass obj = new MyfingerClass;
    Console.WriteLine(matlab.Execute(“tks=obj.demo_fingerprint()”))

    it says “undefined variable “obj” or class “obj.demo_fingerprint” “, where demo_fingerprint is a funtion, that i have geneated in MatLab, from class MyFingerClass

    Can tpu solve it?

     
  56. Andreia

    January 20, 2014 at 5:28 am

    I already solved the problem

    thanks

     
  57. O.Hussain

    May 12, 2014 at 1:31 am

    Hi,

    Can I use the same way in Unity 3D with matlab functions ?

    Thanks alot,

     
  58. xinyustudio

    May 12, 2014 at 5:41 pm

    Yes, sure. No problem at all.

     
  59. O.Hussain

    May 12, 2014 at 10:07 pm

    Hi xinyustudio,

    I tried it but it gives me this error:

    MissingMethodException: Method not found: ‘MathWorks.MATLAB.NET.Utility.MWMCR..ctor’.
    Rethrow as TypeInitializationException: An exception was thrown by the type initializer for Mat_unity.Mat_unity
    testwith_matlab.Update ()

    also i put the dlls from dist folder in the Assets folder of my unity project.

     
  60. xinyustudio

    May 12, 2014 at 10:37 pm

    @o.Hussain, if you have looked through the post from the beginning to the end, you will find there is a solution to it. Specifically, go to (8) and then (2) to see how to troubleshoot this. Good luck.

     
  61. Meysam

    July 12, 2014 at 4:05 pm

    Hi xinyustudio,

    I have a simple function for sample but I have a error with this function! my error is:
    ————————-
    … MWMCR::EvaluateFunction error …
    Too many output arguments..
    ————————-

    my function in matlab file was:
    ————————-
    function [y]=foo(x)
    y=x^2;
    end
    ————————-

    and my code in C#.net is:
    ————————
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using foodll01;
    using MathWorks.MATLAB.NET.Arrays;
    using MathWorks.MATLAB.NET.Utility;

    namespace foo
    {
    class Program
    {
    static void Main(string[] args)
    {
    Class1 css1 = new Class1();
    MWNumericArray w = new MWNumericArray(new Int32());
    w = css1.foo(2); \\error in this line
    Console.WriteLine(w.ToString());
    Console.ReadKey();

    }
    }
    }
    ———————–

    TanX!

     
  62. xinyustudio

    July 13, 2014 at 10:50 am

    [y]=foo(x) should be y=foo(x)? No need to use [] when you return a single output.

     

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 40 other followers

%d bloggers like this: