1

Currently I am trying to call MATLAB directly in Visual Studio, but it seems not working.

To be clear, take the following demo case as an example, i.e. use MATLAB to compute 2+3. It is expected that the result, i.e. ans = 5, should be printed in line printf("%s\n", buf);, which turns out to be empty. By the way, the MATLAB engine is opened (engOpen()) successfully.

#include <stdio.h>
#include <thread>
#include "engine.h"

Engine *matlab;

void thread_func()
{
    // set printing buffer
    char buf[1001]; 
    engOutputBuffer(matlab, buf, 1000);

    // call MATLAB
    engEvalString(matlab, "2+3");
    printf("%s\n", buf); // if ok, should print "ans = 5" in the command window
}

int main()
{
    // Open MATLAB engine
    matlab = engOpen(NULL);
    if (matlab == NULL){
        printf("Error to open MATLAB engine!!!\n");
        exit(-1);
    }

    // use "Engine *matlab" in this thread
    std::thread another_thread(thread_func);

    // wait the thread to finish
    another_thread.join();

    // Close MATLAB engine
    engClose(matlab);

    return 0;
}

More info:

  • MATLAB version: R2014a x64
  • Visual Studio version: 2013 Professional
  • The build platform of the project is also set to x64.
  • MATLAB command window is created while running the code, which is expected.
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174

2 Answers2

1

For Visual Studio 2010.

If you are using Matlab 64, then you need to configure your visual studio to be 64bit.

Step One:

include engine.h

C/C++->General->Additional Include Directories

add C:\Program Files\MATLAB\R2014a\extern\include

Step Two:

Linker->General->Additional Library Directories

add C:\Program Files\MATLAB\R2014a\extern\lib\win64\microsoft

Step Three:

add C:\Program Files\MATLAB\R2014a\bin\win64 in environment variables.

The output of your code is

enter image description here

CroCo
  • 5,531
  • 9
  • 56
  • 88
  • Hi CroCo, fyi, I have finally found the real problem in my case, i.e. trying to create the MATLAB engine in one thread and use it in another, see [my own answer](http://stackoverflow.com/a/26218434/2589776). Also, +1 to your answer, it actually works in single thread style. Thanks again. – herohuyongtao Oct 06 '14 at 14:34
  • Then you need to edit your question so that it reflects multi-thread issue. – CroCo Oct 07 '14 at 18:25
1

Here is a simple example I tried:

test_engine.cpp

#include <cstdio>
#include "engine.h"

#define BUFSIZE 1000

int main()
{
    // open connection
    Engine *matlab = engOpen(NULL);
    if (matlab == NULL) {
        fprintf(stderr, "Error to open MATLAB engine\n");
        return EXIT_FAILURE;
    }

    // output buffer
    char buf[BUFSIZE+1];
    buf[BUFSIZE] = '\0';
    engOutputBuffer(matlab, buf, BUFSIZE);

    // call MATLAB
    engEvalString(matlab, "x = magic(5)");
    
    printf("Output:\n%s\n", buf);

    // close connection
    engClose(matlab);

    return EXIT_SUCCESS;
}

Instead of manually creating a Visual Studio project to compile it, we can do this right from MATLAB:

>> mbuild test_engine.cpp -llibeng -llibmx

in R2014a and up, we can also use:

>> mex -client engine test_engine.cpp

(assuming you've run mex -setup and mbuild -setup to select a proper C++ compiler, or mex -setup C++ and mex -setup C++ -client MBUILD in R2014a).

Here is the output of the program (I'm running R2014a x64 with VS2013):

C:\> test_engine.exe
Output:
x =
    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9

EDIT:

Here is how to compile the above example in Visual Studio.

  1. Start by creating a new empty "Win32 Console Application" project

  2. Since we're working with MATLAB x64, we need to adjust project configuration to generate 64-bit binaries. From "Build" menu, select "Configuration Manager". From the drop-down menu choose <New> to create a new platform configs, select x64 and accept.

  3. Add the source code from the previous example test_engine.cpp

  4. Next step is tell the VC++ compiler/linker where to find MATLAB headers and libraries. We could do this by setting the project properties.

    A better approach is to create a separate property sheet that can be reused from multiple projects without having to repeat the same settings over and over again. So create the MATLAB_Engine.props property sheet shown below, and add it the project (open the "Property Manager" panel and click the "Add Existing Property Sheet" button).

  5. Finally build the solution and run it (Ctrl+F5)

MATLAB_Engine.props

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ImportGroup Label="PropertySheets" />
  <PropertyGroup Label="UserMacros" />
  <PropertyGroup>
    <LocalDebuggerEnvironment>PATH=C:\Program Files\MATLAB\R2014a\bin\win64;%PATH%$(LocalDebuggerEnvironment)</LocalDebuggerEnvironment>
  </PropertyGroup>
  <ItemDefinitionGroup>
    <ClCompile>
      <AdditionalIncludeDirectories>C:\Program Files\MATLAB\R2014a\extern\include;C:\Program Files\MATLAB\R2014a\extern\include\win64</AdditionalIncludeDirectories>
      <PreprocessorDefinitions>IBMPC</PreprocessorDefinitions>
    </ClCompile>
    <Link>
      <AdditionalLibraryDirectories>C:\Program Files\MATLAB\R2014a\extern\lib\win64\microsoft</AdditionalLibraryDirectories>
      <AdditionalDependencies>libmx.lib;libmex.lib;libmat.lib;libeng.lib;mclmcrrt.lib</AdditionalDependencies>
    </Link>
  </ItemDefinitionGroup>
  <ItemGroup />
</Project>

(this is intended for MATLAB R2014a on a 64-bit Windows. Adjust the path if you installed MATLAB to a different location)

The output as expected:

output

Tip: if you are testing an engine program repeatedly, then each time it runs a new MATLAB process opens up and closes in the background. To make things easier during development, you could start a normal MATLAB session, and execute the command below to tell it to act like as an Automation server. That way all Engine programs will run in this same session which remains open.

>> enableservice('AutomationServer',true);

EDIT2:

The MATLAB documentation explicitly states that the Engine library is not thread-safe (same goes for the MEX-API and the MAT-API). In Windows systems, standalone Engine programs communicate with the external MATLAB process through a COM interface, while it uses pipes on Linux/Mac systems as IPC mechanism.

So if you create multi-threaded applications, make sure only one thread accesses the engine application.

Note: For Windows only, there is another function engOpenSingleUse. It differs from engOpen in that it creates a new non-shared MATLAB engine session. That way you can have multiple threads each connected to a different session (obviously workspace is not shared, because each session has a separate address space).

Community
  • 1
  • 1
Amro
  • 123,847
  • 25
  • 243
  • 454
  • Hi Amro, thanks. Actually I can reproduce what you said. But still, I cannot make it work in VC project. Any idea? – herohuyongtao Oct 06 '14 at 02:04
  • @herohuyongtao: it should be no different. see my edit for instructions. – Amro Oct 06 '14 at 11:23
  • Hi Amro, thanks for your patient help. +1 to your very inspiring answer, especially for the last *tip*, which is very useful. FYI, I have finally found the real problem in my case, i.e. trying to create the MATLAB engine in one thread and use it in another, see [my own answer](http://stackoverflow.com/a/26218434/2589776). Thanks again. – herohuyongtao Oct 06 '14 at 14:33
  • @herohuyongtao: ah, well you never mentioned multiple threads! I made another edit. – Amro Oct 06 '14 at 16:22
  • 1
    some related posts: http://stackoverflow.com/a/26063444/97160, http://stackoverflow.com/q/248421/97160, http://stackoverflow.com/q/19250493/97160 – Amro Oct 06 '14 at 16:24