matlab2014调用vs2015进行混合编译生成mex文件

 一、matlab调用vs2015进行混合编译的mex文件

        matlab的版本要到2015b才支持vs2015,当然如果你的matlab版本是2014,不想重装matlab2015也行,需要替换其mexopts文件夹,估计就类似于一个mex的Makefile文件,路径为R2014a\bin\win64\mexopts。下载之,替换即可。mexopts文件夹的下载地址为mexopts点击下载,之后做以下步骤编译即可。

       1、在matlab命令行窗口输入mex -setup

         显示如下:如果你安装了多个版本的VS,选择其中你指定的版本即可。

      2、在matlab命令行窗口执行命令:mex -v yourfile.c(注意:yourfile.c为你想编译的源文件,.cpp文件好像也可以

     例如我的.c文件为decodeBits.c

     执行命令:mex -v decodeBits.c,会出现以下结果,当前目录下会生成一个名为decodeBits.mexw64的文件,在matlab里面就可以调用这个文件了。

     


decodeBits.c

//Author:  David Varodayan (varodayan@stanford.edu)
//Date:    May 8, 2006

#include "mex.h"
#include <stdio.h>
#include <math.h>

#define max(a,b) (((a)>(b))?(a):(b))

//decodeBits() finds the minimum rate for which the decoded bitstream matches
//the transmitted portion of the accumulated syndrome.
//The number of residual bit errors is also calculated.
void decodeBits(double *LLR_intrinsic, double *accumulatedSyndrome, double *source, char *ladderFile,
                double *decoded, double *rate, double *numErrors)
{
    FILE *fp;
    int n, m, nzmax, *ir, *jc;
    int numCodes, totalNumInc, numInc, *txSeq;
    int code, k, currIndex, prevIndex;
    double *syndrome;
    
    fp = fopen(ladderFile, "r");
 
    fscanf(fp, "%d", &numCodes);
    fscanf(fp, "%d", &n);
    fscanf(fp, "%d", &nzmax);
    fscanf(fp, "%d", &totalNumInc);
        
    ir = mxCalloc(nzmax, sizeof(int));
    jc = mxCalloc(n+1, sizeof(int));
    txSeq = mxCalloc(totalNumInc, sizeof(int)); //actual length: numInc
    syndrome = mxCalloc(n, sizeof(double)); //actual length: m
    
    for(k=0; k<n+1; k++)
        fscanf(fp, "%d", jc+k);    
    
    //iterate through codes of increasing rate
    for(code=0; code<numCodes; code++)
    {
        fscanf(fp, "%d", &numInc);
        for(k=0; k<numInc; k++)
            fscanf(fp, "%d", txSeq+k);
        for(k=0; k<nzmax; k++)
            fscanf(fp, "%d", ir+k);
        m = (n/totalNumInc)*numInc;
        
        rate[0] = ((double) m)/((double) n);

        if(rate[0]==1)
        {
            fclose(fp);
            for(k=0; k<n; k++)
                decoded[k]=source[k]; //result of Gaussian elimination
            numErrors[0] = 0;
            return;
        }
        
        currIndex = txSeq[0];
        syndrome[0] = accumulatedSyndrome[currIndex];
        for(k=1; k<m; k++)
        {
            prevIndex = currIndex;
            currIndex = txSeq[k%numInc] + (k/numInc)*totalNumInc;
            syndrome[k] = (double) (((int) (accumulatedSyndrome[currIndex] + accumulatedSyndrome[prevIndex])) % 2);
        }

        if(beliefPropagation(ir, jc, m, n, nzmax, LLR_intrinsic, syndrome, decoded))
        {
            fclose(fp);
            numErrors[0] = 0;
            for(k=0; k<n; k++)
                numErrors[0] += (double) (decoded[k]!=source[k]);
            return;
        }   
    }
}

//For implementation outline of beliefPropagation(), refer to 
//W. E. Ryan, "An Introduction to LDPC Codes," in CRC Handbook for Coding 
//and Signal Processing for Recording Systems (B. Vasic, ed.) CRC Press, 2004.
//available online (as of May 8, 2006) at 
//http://www.ece.arizona.edu/~ryan/New%20Folder/ryan-crc-ldpc-chap.pdf

//beliefPropagation() runs several iterations belief propagation until
//either the decoded bitstream agrees with the transmitted portion of 
//accumulated syndrome or convergence or the max number of iterations.
//Returns 1 if decoded bitstream agrees with 
//transmitted portion of accumulated syndrome.
int beliefPropagation(int *ir, int *jc, int m, int n, int nzmax, 
                       double *LLR_intrinsic, double *syndrome,
                       double *decoded)
{
    int iteration, k, l, sameCount;
    double *LLR_extrinsic, *check_LLR, *check_LLR_mag, *rowTotal, *LLR_overall;
    
    LLR_extrinsic = mxCalloc(nzmax, sizeof(double));
    check_LLR = mxCalloc(nzmax, sizeof(double));
    check_LLR_mag = mxCalloc(nzmax, sizeof(double));
    rowTotal = mxCalloc(m, sizeof(double));    
    LLR_overall = mxCalloc(n, sizeof(double));
    
    sameCount = 0;
    for(k=0; k<n; k++)
        decoded[k] = 0;
    
    //initialize variable-to-check messages
    for(k=0; k<n; k++)
        for(l=jc[k]; l<jc[k+1]; l++)
            LLR_extrinsic[l] = LLR_intrinsic[k];
    
    for(iteration=0; iteration<100; iteration++)
    {
        //Step 1: compute check-to-variable messages
        
        for(k=0; k<nzmax; k++)
        {
            check_LLR[k] = (double) ((LLR_extrinsic[k]<0) ? -1 : 1);
            check_LLR_mag[k] = ((LLR_extrinsic[k]<0) ? -LLR_extrinsic[k] : LLR_extrinsic[k]);
        }
        
        for(k=0; k<m; k++)
            rowTotal[k] = (double) ((syndrome[k]==1) ? -1 : 1);
        for(k=0; k<nzmax; k++)
            rowTotal[ir[k]] *= check_LLR[k];        
        for(k=0; k<nzmax; k++)
            check_LLR[k] = check_LLR[k] * rowTotal[ir[k]];
            //sign of check-to-variable messages
        
        for(k=0; k<nzmax; k++)
            check_LLR_mag[k] = -log( tanh( max(check_LLR_mag[k], 0.000000001)/2 ) );
        for(k=0; k<m; k++)
            rowTotal[k] = (double) 0;
        for(k=0; k<nzmax; k++)
            rowTotal[ir[k]] += check_LLR_mag[k];        
        for(k=0; k<nzmax; k++)
            check_LLR_mag[k] = -log( tanh( max(rowTotal[ir[k]] - check_LLR_mag[k], 0.000000001)/2 ) );
            //magnitude of check-to-variable messages
            
        for(k=0; k<nzmax; k++)
            check_LLR[k] = check_LLR[k] * check_LLR_mag[k];
            //check-to-variable messages
            
        //Step 2: compute variable-to-check messages
        
        for(k=0; k<n; k++)
        {
            LLR_overall[k] = LLR_intrinsic[k];
            for(l=jc[k]; l<jc[k+1]; l++)
                LLR_overall[k] += check_LLR[l];
        }
            
        for(k=0; k<n; k++)
            for(l=jc[k]; l<jc[k+1]; l++)
                LLR_extrinsic[l] = LLR_overall[k] - check_LLR[l];
                //variable-to-check messages
            
        //Step 3: test convergence and syndrome condition
        
        l = 0;
        for(k=0; k<n; k++)
            if(decoded[k] == ((LLR_overall[k]<0) ? 1 : 0))
                l++;
            else
                decoded[k] = ((LLR_overall[k]<0) ? 1 : 0);
        
        sameCount = ((l==n) ? sameCount+1 : 0); 
        
        if(sameCount==5)
            return 0; //convergence (to wrong answer)
        
        for(k=0; k<m; k++)
            rowTotal[k] = syndrome[k];
        for(k=0; k<n; k++)
            for(l=jc[k]; l<jc[k+1]; l++)
                rowTotal[ir[l]] += decoded[k];
                
        for(k=0; k<m; k++)
            if(((int) rowTotal[k] % 2) != 0)
                break;
            else if(k==m-1)
                return 1; //all syndrome checks satisfied
           
    }
    
    return 0;
}

//C-MEX wrapper
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    double *LLR_intrinsic, *accumulatedSyndrome, *source, *decoded, *rate, *numErrors;
    char ladderFile[50];
    FILE *fp;
    int n;
    
    LLR_intrinsic = mxGetPr(prhs[0]);
    accumulatedSyndrome = mxGetPr(prhs[1]);
    source = mxGetPr(prhs[2]);
    mxGetString(prhs[3], ladderFile, 50); 
    
    fp = fopen(ladderFile, "r");
    fscanf(fp, "%d", &n);
    fscanf(fp, "%d", &n);
    fclose(fp);    
    
    plhs[0] = mxCreateDoubleMatrix(1, n, mxREAL);
    decoded = mxGetPr(plhs[0]);
    plhs[1] = mxCreateDoubleMatrix(1, 1, mxREAL);
    rate = mxGetPr(plhs[1]);
    plhs[2] = mxCreateDoubleMatrix(1, 1, mxREAL);
    numErrors = mxGetPr(plhs[2]);
    
    decodeBits(LLR_intrinsic, accumulatedSyndrome, source, ladderFile, decoded, rate, numErrors);
}

   注意:        

       (1).c文件中是必须有下面的函数作为主函数(类似main函数的地位),否则会报类似于“error LNK2001: 无法解析的外部符号 mexFunction“的错误。

   // Matlab interface Function
   // 参数:
   //  nlhs和nrhs表示调用该函数时返回值和参数的个数
   //  prhs[]和plhs[]表示参数和返回值数组
   void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
    {
           ...
    }

       (2)还有需要#include<mex.h>等等。

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
Matlab调用MEX文件,你需要使用mex命令,并在命令中指定相关的选项和参数。根据引用\[1\]和引用\[2\]的内容,以下是一个示例: 要编译一个MEX文件,你需要将matlabroot和arch的值复制到mex命令中,并指定相关的库路径和文件。例如,假设你的matlabroot路径是'C:\Program Files\MATLAB\R2014a',你可以使用以下命令编译一个名为matrixDivide.c的MEX文件mex '-LC:\Program Files\MATLAB\R2014a\extern\lib\win64\microsoft' -llibmwlapack matrixDivide.c 如果你需要处理复数例程,你可以使用-I选项指定包含MATLAB LAPACK库子例程的路径。例如,假设你想要处理复数例程并使用matrixDivideComplex.c示例文件,你可以使用以下命令: copyfile(fullfile(matlabroot,'extern','examples','refbook','matrixDivideComplex.c'),'.','f') mex -I"path_to_lapack_headers" matrixDivideComplex.c 请注意,你需要将"path_to_lapack_headers"替换为LAPACK头文件的实际路径。 如果你需要向mex链接命令添加选项,你可以使用LINKFLAGS命令行选项。例如,在Windows上编译mymex.c并指定可执行文件的环境,你可以使用以下命令: mex -v LINKFLAGS='%LINKFLAGS% /subsystem:windows' mymex.c 这将在编译时将/subsystem:windows选项添加到链接命令中。 总结起来,要在Matlab调用MEX文件,你需要使用mex命令,并根据需要指定相关的选项和参数。具体的命令取决于你的需求和环境设置。 #### 引用[.reference_title] - *1* *2* *3* [Matlabmex的使用方法](https://blog.csdn.net/jk_101/article/details/111254624)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

rs勿忘初心

您的鼓励将是我的最大创动原动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值