在vs2010调用matlab2009引擎

(1)设置环境变量

Win10系统,右键点击此电脑,属性——高级系统设置——环境变量——PATH(双击)——添加Matlab2009a安装路径

(2)打开VS2010,新建一个空项目,右键点击打开属性——VC++目录——包含目录,加上

D:\Matlab\2009a\extern\include

(3)属性——VC++目录——库目录,加上

D:\Matlab\2009a\extern\lib\win32\microsoft

 

测试文档,参考链接:http://blog.sina.com.cn/s/blog_46da01db0100stvb.html

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "engine.h"
#include "matrix.h"

#pragma comment(lib,"libeng.lib") 
#pragma comment(lib,"libmx.lib")

int main()
{
    Engine *ep;
    int i , j ;

    //show how to open MATLAB engine
    //for remote ones:
    //engOpen( ADDRESS OF REMOTE SYSTEM ) ;

    if (!(ep = engOpen("\0"))){
        fprintf(stderr, "\nCan't start MATLAB engine\n");
        return EXIT_FAILURE;
    }

    //show how to create matrix
    mxArray *Y = mxCreateDoubleMatrix(1 , 3 , mxREAL) ;
    
    //show how to put data in matrix
    double tmp[3] = {1.0 , 2.0 , 3.0} ;
    memcpy(mxGetPr(Y) , tmp , sizeof(tmp)) ;

    //show how to put variables in the Engine
    engPutVariable(ep , "Y" , Y) ;

    //show how to execute commands in MATLAB
    engEvalString(ep, "X = ones(5,1) * Y");
    
    //show how to get variables from the Engine
    mxArray *X = engGetVariable(ep , "X") ;
    
    //show how to manipulate dimensions
    int dims[10] ;
    int ndims ;
    ndims = mxGetNumberOfDimensions(X) ;
    printf("total number of dimensions is %d\n" , ndims) ;
    memcpy(dims , mxGetDimensions(X) , ndims * sizeof(int)) ;
    for ( i = 0 ; i < ndims ; i ++ ){
        printf("dimension %d : %d\n" , i , dims[i]) ;
    }
    printf("\n") ;

    //show how the data is stored in the memory
    double *p = (double*)mxGetData(X) ;    
    for ( i = 0 ; i < dims[0] ; i ++ ){
        for ( j = 0 ; j < dims[1] ; j ++ ){
            printf("%8.2f" , p[j * dims[0] + i]) ;
        }
        printf("\n") ;
    }

    //---important, to release resources
    mxDestroyArray(X) ;
    mxDestroyArray(Y) ;

    //show how to hide and unhide MATLAB command window
    printf("type RETURN to hide the MATLAB command window...\n") ;
    getchar() ;
    engSetVisible(ep , false) ;
    printf("type RETURN to unhide the MATLAB command window...\n") ;
    getchar() ;
    engSetVisible(ep , true) ;

    printf("type RETURN to END this program...\n") ;
    getchar() ;    
    //remembering to close it is important .
    //but if you are debugging your programs , 
    //annotate the following line will save you a lot of time ,
    //for you needn't to restart the Engine .
    engClose(ep) ;
    
    //when your work is accomplished , type "exit" in MATLAB command window

    return EXIT_SUCCESS;
}

 


》》某些问题

如果出现这个:

engdemo.obj : error LNK2001: unresolved external symbol _engClose
engdemo.obj : error LNK2001: unresolved external symbol _engSetVisible
engdemo.obj : error LNK2001: unresolved external symbol _mxDestroyArray
engdemo.obj : error LNK2001: unresolved external symbol _mxGetData
engdemo.obj : error LNK2001: unresolved external symbol _mxGetDimensions_730
engdemo.obj : error LNK2001: unresolved external symbol _mxGetNumberOfDimensions_730
engdemo.obj : error LNK2001: unresolved external symbol _engGetVariable
engdemo.obj : error LNK2001: unresolved external symbol _engEvalString
engdemo.obj : error LNK2001: unresolved external symbol _engPutVariable
engdemo.obj : error LNK2001: unresolved external symbol _mxGetPr
engdemo.obj : error LNK2001: unresolved external symbol _mxCreateDoubleMatrix_730
engdemo.obj : error LNK2001: unresolved external symbol _engOpen

其实就是lib没有添加好。

在代码中写上:
#pragma comment(lib,"libeng.lib") 
#pragma comment(lib,"libmx.lib")
就可以了。

或者可以在工程的连接设置里面添加这两个库。
不过我倾向于前者,这样在发布源码的同时,
就尽最大可能地保证能够编译,
而不用其他人学习的时候再去设置。

当然,由于#pragma是由编译器自己决定的,
所以代码的可移植性存在一些问题。

如果还是报上面的错误,估计是没有将lib的路径添加对。
具体参考上面的那个实例,然后注意把路径换成自己机器上的。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值