zoukankan      html  css  js  c++  java
  • 【MATLAB与C的混合编程】之【MATLAB调用C程序】

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

  • 相关阅读:
    js 生成随机数
    解决微信浏览器无法使用reload()刷新页面
    js 去除左右空格
    小程序开发入门-第一天
    我的第一个JSP——动态web
    2019-3-6 复制粘贴
    2019-2-19 异常练习
    2019-1-19 object祖宗类的equals重写
    2019-1-15 课堂笔记
    2019-1-15 课后作业
  • 原文地址:https://www.cnblogs.com/caixu/p/2087919.html
Copyright © 2011-2022 走看看