C语言调用Matlab

开发环境 : Windows XP SP2

IDE        : Codeblocks

Matlab   : 7.0

程序实现一个简单的FFT运算,最后把结果返回C程序

把 D:/MATLAB7/extern/include 添加到Search directories

把 D:/MATLAB7/extern/lib/win32/microsoft/msvc71 添加到 Link settings

程序:

#include
#include
#include
#include
#include

#define PI  3.1415926
#define N   32
#define F   100

int main()
{
    mxArray *my = NULL;
    Engine *ep;
    double y[N];
    int i;

    ep=engOpen("/0");
    if(!ep){
        printf("Faild in opening matlab engine");
        return EXIT_FAILURE;
    }
    for(i=0;i        y[i]=sin(2*PI*5*i/F);
    }
    my=mxCreateDoubleMatrix(1,N,mxREAL);
    memcpy((void*)mxGetPr(my),(void*)y,sizeof(y));
    engPutVariable(ep, "my",my);
    mxDestroyArray(my);
    engEvalString(ep, "my=fft(my);");
    engEvalString(ep, "my=abs(my);");
    engEvalString(ep, "my=abs(my);");
    my=engGetVariable(ep,"my");
    memcpy((void*)y,(void*)mxGetPr(my),sizeof(y));
    mxDestroyArray(my);
    engClose(ep);
    for(i=0;i        printf("%lf/n",y[i]);
    }
    printf("Hello world!/n");
    return 0;
}

 

在Matlab中有详细的帮助文档

点击Help,选择Contents选项卡

依次点击展开 Matlab、External Interface 、Calling Matlab from C and Fortran Programs

你可能感兴趣的:(c/c++)