【MATLAB与C的混合编程】之【MATLAB调用C程序】

62 篇文章 2 订阅
在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<M;i++)
  {
     for(j=0;j<N;j++)mexPrintf("%4.3f  ",data[j*M+i]);//data[j*M+i]值得玩味
     mexPrintf("\n");
  }
/*for(i=0;i<N*M;i++)mexPrintf("%4.3f  ",data[i]);
mexPrintf("\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<M;i++)
    for(j=0;j<N;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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值