There are two ways in VC++ to call MATLAB:
distribute MATLAB code into MATLAB independent C++ shared library. Refer to the previous blog for details.
call MATLAB directly in VC++.
This blog will focus on the second approach (also the easier one).
Pre-work:
- Install MATLAB: unlike the first method, make sure you have the whole MATLAB application installed in order to call MATLAB directly in VC++ (only MATLAB Compiler Runtime is needed for the first method). We will take MATLAB-x64-R2013a as an example (installed under
C:\Program Files\MATLAB\R2013a
). - Platform consistence: the platforms of MATLAB and VC++ compile platform must be the same, i.e. Win32/x86 VC++ compile platform can only use x86 MATLAB and x64 VC++ compile platform can only use x64 MATLAB.
- Example: take the following example
myadd2.m
(assume underC:\
):1
2
3
4
5
6function [y, z] = myadd2(a, b)
% dummy function, just to demonstrate the idea
y = a+b;
z = a+2*b;
disp('Output from MATLAB.');
end
VC setup:
- Add MATLAB include folder to project
Include
files: i.e.C:\Program Files\MATLAB\R2013a\extern\include
- Add MATLAB library folder to project
Library
files: i.e.C:\Program Files\MATLAB\R2013a\extern\lib\win64\microsoft
- Add relevant libraries to
Linker>Input>Additional Dependencies
:libmx.lib libeng.lib
- Add MATLAB Compiler Runtime binary folder to project
Debugging>Environment
: i.e.PATH=%PATH%;$(ProjectDir)\dlls_x64;C:\Program Files\MATLAB\R2013a\runtime\win64;C:\Program Files\MATLAB\R2013a\bin\win64
- Add header to main code:
#include "engine.h"
Call in VC++: refer to VC_call_MATLAB_directly.cpp for details.
In this cpp file, there are also ways to handle data passing of different types between VC++ and MATLAB (see the
SHOW_EXTEND_INFO
part), which includes:double
(orint
/float
/…)string
- matrix
- gray-scale image
- RGB image
Other Remarks:
- Try to close the previous MATLAB window first (extra command window) if stuck at
engOpen(NULL)
.