1、启动MATLAB7,新建m文件,输入:
 function myfun(n)
function myfun(n) t=0:n;
t=0:n; y=sin(t);
y=sin(t); plot(y);
plot(y);保存文件为:myfun.m
2、在Matlab 的Command Window 下输入命令:mcc -B csglsharedlib:mylib myfun 生成动态链接库DLL。Matlab 会生成一系列文件, 其中mylib.h mylib.lib mylib.dll mylib.ctf是我们这里需要的。
3、 在VC++中使用Matlab 生成的动态链接库( *.DLL)
以上面创建的TestDllApp工程为例。将mylib.h mylib.lib mylib.dll mylib.ctf文件拷贝至TestDllApp工程目录下并添加入工程。修改TestDllApp.cpp文件:
 // TestDllApp.cpp : Defines the entry point for the console application.
// TestDllApp.cpp : Defines the entry point for the console application.
 //
// #include "stdafx.h"
#include "stdafx.h" #include <stdio.h>
#include <stdio.h> #include "mylib.h"
#include "mylib.h" #include "mclmcr.h"
#include "mclmcr.h"
 int opp(int n);
int opp(int n); void main(int argc, char* argv[])
void main(int argc, char* argv[]) {
{ mylibInitialize();
    mylibInitialize(); double x=64;
    double x=64; mxArray *t;
    mxArray *t; t=mxCreateDoubleMatrix(1,1,mxREAL);
    t=mxCreateDoubleMatrix(1,1,mxREAL); memcpy(mxGetPr(t),&x,sizeof(double));
    memcpy(mxGetPr(t),&x,sizeof(double)); mlfMyfun(t);
    mlfMyfun(t); mxDestroyArray(t);
    mxDestroyArray(t); mylibTerminate();
    mylibTerminate(); printf("This is a test!\n");
    printf("This is a test!\n"); printf("%d\n",opp(16));
    printf("%d\n",opp(16));  }
} int opp(int n){
int opp(int n){ return n/4*4+4;
    return n/4*4+4; }
}
4、通过菜单工程/设置,打开工程设置属性页,进入Link页面,在Object/library modules编辑框中,添加文件名libmx.lib libmat.lib libeng.lib。
5、编译运行程序。
 
                     
                    
                 
                    
                 
        
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号