win10专业版通知模式:【MATLAB与C的混合编程】之【MATLAB调用C程序】

来源:百度文库 编辑:中财网 时间:2024/05/05 18:33:03
在MATLAB中配置C编译器,命令mex -setup

1)提示Would you like mex to locate installed compilers [y]/n?选n

2)提示Compiler:选8 (注:Microsoft Visual C++ 2008 SP1)

3)提示Use C:\Program Files\Microsoft Visual Studio 9.0 anyway [y]/n?选n

4)提示Please enter the location of your compiler: [C:\Program Files\Microsoft Visual Studio 9.0]选D:\Program Files\Microsoft Visual Studio 9.0

5)确认,安装编译器成功

现在开始MATLAB与C的混合编程之旅!

先普及知识:

函数mexFunction(输出参数个数,输出参数指针,输入参数个数,输入参数指针)============================================================

/*hello.c*/
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
  mexPrintf("hello,world!\n");
}

//先输入mex hello.c进行编译,你会看到目录下多出一个hello.mexw32文件(下面例子不再给出这句注释)
//调用:hello
//显示:hello,world!

============================================================

//hello.c 2.0
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
  int i;
  i=mxGetScalar(prhs[0]);
  if(i==1)
    mexPrintf("hello,world!\n");
  else
    mexPrintf("大家好!\n");
}
//调用:hello
//显示:大家好!

//调用:hello(1)
//显示:hello,world!

//调用:hello(2)
//显示:大家好!

============================================================

//hello.c 2.1
#include "mex.h"
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
  int *i;
  i=mxGetPr(prhs[0]);//指向输入矩阵的地址
  if(i[0]==1)
    mexPrintf("hello,world!\n");
  else 
    mexPrintf("大家好!\n");
}
//调用:a=1:10;hello(a)
//显示:hello,world!

//调用:c=2:11;hello(c)
//显示:大家好!

============================================================

===================函数mxGetPr、mxGetM、mxGetN====================

============================================================

//show.c 1.0
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
  double *data;
  int M,N;
  int i,j;
  data=mxGetPr(prhs[0]); //获得指向矩阵的指针
  M=mxGetM(prhs[0]); //获得矩阵的行数
  N=mxGetN(prhs[0]); //获得矩阵的列数
  for(i=0;i  {
     for(j=0;j     mexPrintf("\n");
  }
/*for(i=0;imexPrintf("\n");*/
}
//调用:a=1:10;b=[a;a+1];show(a)
//显示:1.000  2.000  3.000  4.000  5.000  6.000  7.000  8.000  9.000  10.000 
//继续输入:show(b)
//显示:
//1.000  2.000  3.000  4.000  5.000  6.000  7.000  8.000  9.000  10.000  
//2.000  3.000  4.000  5.000  6.000  7.000  8.000  9.000  10.000  11.000

============================================================

=====================函数mxCreateDoubleMatrix======================

============================================================

//reverse.c 1.0
#include "mex.h"
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
  double *inData;
  double *outData;
  int M,N;
  int i,j;
  inData=mxGetPr(prhs[0]);
  M=mxGetM(prhs[0]);
  N=mxGetN(prhs[0]);
  plhs[0]=mxCreateDoubleMatrix(M,N,mxREAL);
  outData=mxGetPr(plhs[0]);
  for(i=0;i    for(j=0;j     outData[j*M+i]=inData[j*M+i]+10;
}
//调用:r=reverse(b)
//显示:11    12    13    14    15    16    17    18    19    20
//    12    13    14    15    16    17    18    19    20    21