问题
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.
回答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.
Start by creating a new empty "Win32 Console Application" project
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, selectx64
and accept.Add the source code from the previous example
test_engine.cpp
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).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:
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).
回答2:
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
来源:https://stackoverflow.com/questions/26199375/call-matlab-directly-multiple-threading-in-visual-studio