• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

首先,在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

【引文】http://blog.csdn.net/sbtdkj1017/archive/2007/11/25/1901647.aspx


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Delphi web应用开发框架 - 第二部发布时间:2022-07-18
下一篇:
Delphi图像处理 -- 图像颜色矩阵调整发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap