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).
- 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
- 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
function [y, z] = myadd2(a, b) % dummy function, just to demonstrate the idea y = a+b; z = a+2*b; disp('Output from MATLAB.'); end
- Add MATLAB include folder to project
- Add MATLAB library folder to project
- Add relevant libraries to
- Add MATLAB Compiler Runtime binary folder to project
PATH=%PATH%;$(ProjectDir)\dlls_x64;C:\Program Files\MATLAB\R2013a\runtime\win64;C:\Program Files\MATLAB\R2013a\bin\win64
- Add header to main code:
Call in VC++: refer to
In this cpp file, there are also ways to handle data passing of different types between VC++ and MATLAB (see the
SHOW_EXTEND_INFOpart), which includes:
- gray-scale image
- RGB image
NULL, you should register MATLAB first by1
- Run DOS in admin mode.
- In DOS, go to MATLAB install folder that contains
- Run command:
Note: it seems that given MATLAB can not both register both x86 & x64 (e.g. can only register MATLAB-2015b-x86 or MATLAB-2015b-x64, but not both). On the other hand, if we do want both x86 and x64 modes, we can install one MATLAB for one mode and one for another, e.g. MATLAB-2015b-x86 and MATLAB-2014b-x64.
Try to close the previous MATLAB window first (extra command window) if stuck at
engOpen()always returns 0: http://www.mathworks.com/matlabcentral/newsreader/view_thread/238828 ↩