Tuesday 7 July 2015

Integrating MATLAB and Visual Studio using Engine API

This one made me scratch my head to the scalp; literally. So I am going to provide a thorough tutorial on "Using MATLAB Engine API for C++(C)".

TUTORIAL 1 : SETTING IT ALL UP

Requirements :

1. Microsoft Visual Studio (This tutorial will be based on version 2010, moreover they are all similar)
2. MATLAB

Things to know before starting?

  • Find out on what platform is your MATLAB running, that is 32 bit or 64 bit. To do so, go to Start > Right Click on Computer >Properties > Advanced system settings > Environment Variables > In System Variables scroll down to Path > Double click and check whether it has MATLAB bin under win64 directory or win32.

Steps :

  1. Open Visual Studio and create a new project. You might probably choose "Win32 Console Application" and check the "Empty Project" option in dialog box.
  2. Visual Studio by default runs on a 32 bit platform. If your MATLAB is running on a 64 bit platform (most probably this will be the case) then you will have to make sure that VS(Visual Studio) is going to build applications on a 64 bit platform otherwise skip this step. To do so, click on Project menu > project-name properties (this will open Property Pages dialog box)> Configuration Properties > Configuration Manager > Click on drop down menu of "Active solution platform" > New > from drop down select x64 > close and click ok.
  3. Now your VS is capable of building files on 64bit platform. Open Property Pages and select VC++ directories and make sure you have something like "$(VCInstallDir)bin\x86_amd64;" at the beginning, which ensures the 64 bit platform if your MATLAB is on 64 bit too.
  4. In the Property Pages, click on Debugging and in the Environment field add this,

    PATH=(MATLAB installation directory)\bin\win64
  5. In the Property Pages, click on VC++ directories and add the path to include directories by opening,

     (MATLAB installation directory)\extern\include

    I will suggest copy pasting the path from properties dialog box.
  6. Now add library path in Library directories field by opening,

    (MATLAB installation directory)\extern\lib\win64[or win32, whatever you are working on]\microsoft
  7. Now you have to add the executable directories which is as follows,

    (MATLAB installation directory)\bin
  8. Now click on Linker on the left side pane and add the same library path to the "Additional Library Directories" field. Make sure "Enable Incremental Linking" is set to "No" (It pops some error that I don't know why but read this fix somewhere and it works).
  9. In the Linker dropped down, select Input and add the following libraries by clicking on "Additional Dependencies" and move cursor to end and write with semicolons after every library as follows,

    libmx.h;libmex.h;libmat.h;libeng.h;
  10. Hit OK!
If all went well then this is it. Now you are ready to use Engine functions to call MATLAB from Visual Studio.

I will post the next part of this tutorial about writing a Engine file and calling MATLAB soon. If you face problems in setting this up, comment out and I will try to solve because I skipped few minor things.

To check it's working you can go to MATLAB documentation, search for MATLAB engine API and open C,C++ and Fortran and copy paste an example in VS project and see if it works!

No comments:

Post a Comment