Welcome to mbuild -setup. This utility will help you set up
a default compiler. For a list of supported compilers, see
http://www.mathworks.com/support/compilers/R2011b/win32.html
Please choose your compiler for building standalone MATLAB applications:
Would you like mbuild to locate installed compilers [y]/n? y (注:若是第一次mbuild -setup,选n,再在给出的的选项中选择2010 compiler,要注意安装路径是否相符)
Select a compiler:
[1] Microsoft Software Development Kit (SDK) 7.1
[2] Microsoft Visual C++ 6.0
[3] Microsoft Visual C++ 2008 SP1
[4] Microsoft Visual C++ 2010
[5] Microsoft Visual C++ 2012
[0] None
Compiler: 4
Your machine has a Microsoft Visual C++ 2010 compiler located at
E:\Program Files\VS2010. Do you want to use this compiler [y]/n? y
Please verify your choices:
Compiler: Microsoft Visual C++ 2010
Location: E:\Program Files\VS2010
Are these correct [y]/n? y
****************************************************************************
Warning: Applications/components generated using Microsoft Visual C++
2010 require that the Microsoft Visual Studio 2010 run-time
libraries be available on the computer used for deployment.
To redistribute your applications/components, be sure that the
deployment machine has these run-time libraries.
****************************************************************************
Trying to update options file: C:\Users\Yanlu\AppData\Roaming\MathWorks\MATLAB\R2013a\compopts.bat
From template: E:\PROGRA~1\MATLAB~1\bin\win32\mbuildopts\msvc100compp.bat
Welcome to mex -setup. This utility will help you set up
a default compiler. For a list of supported compilers, see
http://www.mathworks.com/support/compilers/R2011b/win32.html
Please choose your compiler for building MEX-files:
Would you like mex to locate installed compilers [y]/n? y
Select a compiler:
[1] Lcc-win32 C 2.4.1 in D:\PROGRA~1\MATLAB\R2011b\sys\lcc
[2] Microsoft Visual C++ 2010 in D:\Program Files\Microsoft Visual Studio 10.0
[3] Microsoft Visual C++ 2008 SP1 in E:\Program Files\Microsoft Visual Studio 9.0
[0] None
Compiler: 2
Please verify your choices:
Compiler: Microsoft Visual C++ 2010
Location: E:\Program Files\VS2010
Are these correct [y]/n? y
***************************************************************************
Warning: MEX-files generated using Microsoft Visual C++ 2010 require
that Microsoft Visual Studio 2010 run-time libraries be
available on the computer they are run on.
If you plan to redistribute your MEX-files to other MATLAB
users, be sure that they have the run-time libraries.
***************************************************************************
Trying to update options file: C:\Users\Yanlu\AppData\Roaming\MathWorks\MATLAB\R2013a\mexopts.bat
From template: E:\PROGRA~1\MATLAB~1\bin\win32\mexopts\msvc100opts.bat
Done . . .
**************************************************************************
Warning: The MATLAB C and Fortran API has changed to support MATLAB
variables with more than 2^32-1 elements. In the near future
you will be required to update your code to utilize the new
API. You can find more information about this at:
http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html
Building with the -largeArrayDims option enables the new API.
**************************************************************************
// MatlabTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include
#include
#include "engine.h"
#include "matrix.h"
#pragma comment(lib,"libeng.lib")
#pragma comment(lib,"libmx.lib")
int main()
{
Engine *ep;
int i , j ;
//show how to open MATLAB engine
//for remote ones:
//engOpen( ADDRESS OF REMOTE SYSTEM ) ;
if (!(ep = engOpen("\0"))){
fprintf(stderr, "\nCan't start MATLAB engine\n");
return EXIT_FAILURE;
}
//show how to create matrix
mxArray *Y = mxCreateDoubleMatrix(1 , 3 , mxREAL) ;
//show how to put data in matrix
double tmp[3] = {1.0 , 2.0 , 3.0} ;
memcpy(mxGetPr(Y) , tmp , sizeof(tmp)) ;
//show how to put variables in the Engine
engPutVariable(ep , "Y" , Y) ;
//show how to execute commands in MATLAB
engEvalString(ep, "X = ones(5,1) * Y");
//show how to get variables from the Engine
mxArray *X = engGetVariable(ep , "X") ;
//show how to manipulate dimensions
int dims[10] ;
int ndims ;
ndims = mxGetNumberOfDimensions(X) ;
printf("total number of dimensions is %d\n" , ndims) ;
memcpy(dims , mxGetDimensions(X) , ndims * sizeof(int)) ;
for ( i = 0 ; i < ndims ; i ++ ){
printf("dimension %d : %d\n" , i , dims) ;
}
printf("\n") ;
//show how the data is stored in the memory
double *p = (double*)mxGetData(X) ;
for ( i = 0 ; i < dims[0] ; i ++ ){
for ( j = 0 ; j < dims[1] ; j ++ ){
printf("%8.2f" , p[j * dims[0] + i]) ;
}
printf("\n") ;
}
//---important, to release resources
mxDestroyArray(X) ;
mxDestroyArray(Y) ;
//show how to hide and unhide MATLAB command window
printf("type RETURN to hide the MATLAB command window...\n") ;
getchar() ;
engSetVisible(ep , false) ;
printf("type RETURN to unhide the MATLAB command window...\n") ;
getchar() ;
engSetVisible(ep , true) ;
printf("type RETURN to END this program...\n") ;
getchar() ;
//remembering to close it is important .
//but if you are debugging your programs ,
//annotate the following line will save you a lot of time ,
//for you needn't to restart the Engine .
engClose(ep) ;
//when your work is accomplished , type "exit" in MATLAB command window
return EXIT_SUCCESS;
}
运行结果:
若出现如下问题:
error C2371: 'char16_t' : redefinition; different basic types
在混合编程中,也许会出现如下错误:
—————————————————————————————————————————————————
C:Microsoft Visual Studio 10.0VCINCLUDEyvals.h(576) : error C2371: 'char16_t' : redefinition; different basic types
C:Matlabr2010aexternincludematrix.h(330) : see declaration of 'char16_t'
—————————————————————————————————————————————————
原因是VS2010中的yvals.h添加了char16_t的定义,而Matlab的matrix.h也包含对char16_t的定义,所以同时包含这两个头文件的话,会导致重复定义char16_t的错误。
解决方案:
参考:http://maciejgryka.com/bits/2011/09/char16_t-conflict-while-mexing-with-vs2010/
完全没有必要修改这两个头文件(以防修改之后,会在将来导致一些问题),只需要在包含matrix.h之前包含yvals.h即可。例如:
#include
#if (_MSC_VER >= 1600)
#define __STDC_UTF_16__
#endif
#include "mex.h"
注:mex.h 包含matrix.h。这就要求我们必须知道所包含的头文件是否包含matrix.h。