美文网首页
MATLAB编程与应用系列-第16章 外部接口技术(2)

MATLAB编程与应用系列-第16章 外部接口技术(2)

作者: 德特数据 | 来源:发表于2018-10-31 22:47 被阅读0次

    本章教程共分为7篇进行阐述:
    MATLAB编程与应用系列-第16章 外部接口技术(1)
    MATLAB编程与应用系列-第16章 外部接口技术(2)
    MATLAB编程与应用系列-第16章 外部接口技术(3)
    MATLAB编程与应用系列-第16章 外部接口技术(4)
    MATLAB编程与应用系列-第16章 外部接口技术(5)
    MATLAB编程与应用系列-第16章 外部接口技术(6)
    MATLAB编程与应用系列-第16章 外部接口技术(7)

    本系列教程来源于出版书籍《基于MATLAB编程基础与典型应用书籍》,如涉及版权问题,请联系:156204968@qq.com。 出版社:人民邮电出版社, 页数:525。

    本系列教程目前基于MATLABR2006a,可能对于更高级版本的功能和函数有差异,教程中如有问题,请联系:156204968@qq.com

    16.2.2 编写C语言MEX文件

    在了解了C语言MEX文件的基本结构,扩展名以及MEX函数库函数和MX函数库函数后,在本小节中,首先对MATLAB编写C语言MEX文件的环境进行配置,然后通过两个简单的MEX文件实例来演示C语言MEX文件的编写。

    1. MEX文件配置

    为了使用不同语言,例如C, C++,或者FORTRAN编写MEX文件,通过MATLAB应用程序接口编译成MATLAB可执行文件,需要对MATLAB编译环境进行配置。成功配置MEX开发环境需要具备两个条件:

    • MATLAB开发环境和应用程序接口(API)组件和MATLAB接口程序编译器(Compiler);
    • C, C++或者FORTRAN语言的编译器,比如Visual Studio C++, FORTRAN等不同语言编译器环境。

    在MATLAB命令行窗口中输入:

    >> mex -setup
    Please choose your compiler for building external interface (MEX) files: 
    
    Would you like mex to locate installed compilers [y]/n? y
    
    Select a compiler: 
    [1] Lcc C version 2.4.1 in C:\PROGRAM FILES\MATLAB\R2006A\sys\lcc 
    [2] Microsoft Visual C/C++ version 6.0 in C:\Program Files\Microsoft Visual Studio 
    
    [0] None 
    
    Compiler: 2
    
    Please verify your choices: 
    
    Compiler: Microsoft Visual C/C++ 6.0 
    Location: C:\Program Files\Microsoft Visual Studio 
    
    Are these correct?([y]/n): y
    
    Trying to update options file: 
    C:\Documents and Settings\Motor308\Application Data\MathWorks\MATLAB\R2006a\mexopts.bat 
    From template:              
    C:\PROGRAM FILES\MATLAB\R2006A\BIN\win32\mexopts\msvc60opts.bat
    

    这样就基本完成了MATLAB编译器C语言接口的配置。然后可以通过编写简单的MEX文件来验证编译器配置正确与否。

    2. MEX编程实例演示

    完成MEX环境配置后,就可以编写简单的MEX文件进行环境测试。首先编写一个实现MATLAB开发环境中ones(M,N)函数功能的MEX文件。

    /*
     * mexSimpleDemo2.c
    */
    
    #include "mex.h"
    int mexSimpleDemo2(double y[], int m_size, int n_size);
    
    void mexFunction(int nlhs, mxArray *plhs[],
                      int nrhs, const mxArray *prhs[])
    {
        //变量声明
        double *y;
        int m,n;
        
        //输入输出参数个数检测
        if (nlhs != 1)
            mexErrMsgTxt("Error: At least one output parameter is required!");
        if (nrhs > 2)
            mexErrMsgTxt("Error: No more than two input parameters");
                
        //获取输入数组大小
        m = mxGetScalar( prhs[0] );
        n = mxGetScalar( prhs[1] );
        
       //获取输出数组的指针和建立初始值。
        plhs[0] = mxCreateDoubleMatrix(m,n,mxREAL);
        y = mxGetPr(plhs[0]);
        
        //调用子函数
        mexSimpleDemo2(y, m ,n);
        
    }
    
    //算法子程序
    int mexSimpleDemo2(double *y, int m_size, int n_size)
    {
        int i,j;
        for( i = 0; i < m_size; i++)
            for( j = 0; j < n_size; j++)
                *y++ = 1;
        
        return(EXIT_SUCCESS);
    }
    

    编译该程序后,在Matlab命令行窗口中输入以下命令:

    >> mexSimpleDemo2(5,3)      %输出参数不匹配,导致错误
    ??? Error: At least one output parameter is required!
    >> c=mexSimpleDemo2(5,3,6)  %输入参数不匹配,导致输入参数检测错误
    ??? Error: No more than two input parameters
    >> c=mexSimpleDemo2(3,4)    %mex文件的正确调用格式
    
    c =
    
         1     1     1     1
         1     1     1     1
         1     1     1     1
    

    3. 字符串操作

    在上一个实例中,可以看到MEX文件的编写结构清晰,实现特定功能的C语言算法子函数完全采用C语言函数库进行,通过编写mexFunction函数接口实现在MATLAB中调用C语言函数。在本实例中,将演示MEX文件进行字符串操作,统计字符串中特定字符的个数。

    /*
     *  mexSimpleDemo3.c
      */
    #include "mex.h"
    int mexSimpleDemo3( char *input_buf, int buf_len);
    
    void mexFunction(int nlhs, mxArray *plhs[],
                                     int nrhs, const mxArray *prhs[])
    {
        //variables declaration
        char *input_buf;
        int count, buf_len,flag;
        
        //输出参数个数统计,没有输出参数。
        if (nlhs >= 1)
            mexErrMsgTxt("Error: No output parameter is required!");
        //输入参数类型检测,输入必须为字符串类型。
        if ( mxIsChar(prhs[0]) != 1)
            mexErrMsgTxt("Error: Input must be string!");
        if ( mxGetM(prhs[0]) != 1)
            mexErrMsgTxt("Error: Input must be a row vector!");
        
        //统计输入字符串的长度
        buf_len = (mxGetM(prhs[0]) * mxGetN(prhs[0])) + 1;
        
        //给输入字符串分配内存空间
        input_buf = mxCalloc(buf_len, sizeof(char));
        flag = mxGetString(prhs[0], input_buf, buf_len);
        
        if( flag != 0)
            mexWarnMsgTxt("Not enough memory space!");
        
        // 调用字符串统计子函数统计字母s(S)的个数
        count = mexSimpleDemo3(input_buf, buf_len);
        mexPrintf("Then number of character s(S) in the input string is %d.",count);
        
    }
    
    //字符串统计子函数统计字母s(S)的个数子函数
    int mexSimpleDemo3( char *input_buf, int buf_len)
    {
        int i, count;
        count = 0;
        for( i = 0; i < buf_len; i++)
            if((*(input_buf + i) == 's')||(*(input_buf + i) == 'S'))
                count ++;
        
        return count;
    }
    

    编译该程序后,在Matlab命令行窗口中输入以下命令:

    >> mexSimpleDemo3(1)                            %输入参数类型不匹配,导致错误
    ??? Error: Input must be string!
    >> c=mexSimpleDemo3('This is a simple demo to mex files!')  %输出参数个数不正确,导致错误
    ??? Error: No output parameter is required!
    >> mexSimpleDemo3('This is a simple demo to mex files!')    %正确的mex文件调用格式
    Then number of character s(S) in the input string is 4.>>
    

    作者:德特数据
    联系方式:156204968@qq.com

    相关文章

      网友评论

          本文标题:MATLAB编程与应用系列-第16章 外部接口技术(2)

          本文链接:https://www.haomeiwen.com/subject/tiwztqtx.html