vc++调用MATLAB complier生成的C动态链接库

来源:互联网 发布:辐射避难所 隐藏数据 编辑:程序博客网 时间:2024/05/17 23:00

相信步骤大家都知道,下面上成果

 

 

 

 

 

 

代码如下,英文系统没有中文输入法,而且打中文居然是乱码,不知道咋整,只能用英文注释了

 

/*the key ideal is to call the function "mlfMrank" which is created by matlab complier,
and i used the command "mcc -B csharedlib:test mrank.m" to generate the c share libaray
which include .dll .h .lib .ctf and so on*/
#include "stdio.h"
#include "test.h"//just with this two header is enough!

void display(const mxArray* in, const char* name);

int main()
{
    //initialize the application
    if( ! mclInitializeApplication(NULL,0) )
    {
        fprintf(stderr, "Could not initialize the application./n");
        exit(1);
    }
   
    //initialize the library
    if (!testInitialize())
    {
        fprintf(stderr,"Could not initialize the library./n");
        exit(1);
    }

    DOUBLE x=10;
    mxArray *X,*Y=NULL;//declare input variable x and output variable y that dll function is needed
    X=mxCreateDoubleMatrix(1,1,mxREAL);//apply for rooms
    memcpy(mxGetPr(X),&x,1*sizeof(double));//assignment

    mlfMrank(1,&Y,X);//you could cheak the definition of the function in header file
                    //"mlfMrank(int nargout, mxArray** r, mxArray* n)"
    display(Y,"Y");

    mxDestroyArray(X);
    mxDestroyArray(Y);

    testTerminate();
    mclTerminateApplication();
    return 0;
}

void display(const mxArray* in, const char* name)
{
    int i,j,r,c;
    double *data;
   
    data = mxGetPr(in);
    r = mxGetM(in);
    c = mxGetN(in);
   
    printf("%s = /n",name);
    for(i=0;i<r;i++)
    {
        printf("/t");
        for(j=0;j<c;j++)
            printf("% 4.2f /t",data[j*r+i]);
        printf("/n");
    }
    printf("/n");

}