MATLAB方面的操作
1.先要完成Matlab C/C++编译器的设置,在matlab中:
>> mbuild -setup
Please choose your compiler for building standalone MATLAB applications:
Would you like mbuild to locate installed compilers [y]/n? n
Select a compiler:
[1] Lcc-win32 C 2.4.1
[2] Microsoft Visual C++ 6.0
[3] Microsoft Visual C++ .NET 2003
[4] Microsoft Visual C++ 2005
[5] Microsoft Visual C++ 2005 Express Edition
[6] Microsoft Visual C++ 2008
[0] None
Compiler: 6
The default location for Microsoft Visual C++ 2008 compilers is C:\Program Files\Microsoft Visual Studio 9.0,
but that directory does not exist on this machine. 
Use C:\Program Files\Microsoft Visual Studio 9.0 anyway [y]/n? n
Please enter the location of your compiler: [C:\Program Files\Microsoft Visual Studio 9.0] D:\Program Files\Microsoft Visual Studio 9.0
Please verify your choices:
Compiler: Microsoft Visual C++ 2008 
Location: D:\Program Files\Microsoft Visual Studio 9.0
Are these correct [y]/n? y
****************************************************************************
  Warning: Applications/components generated using Microsoft Visual Studio 
          2008 require that the Microsoft Visual Studio 2008 run-time     
          libraries be available on the computer used for deployment.     
          To redistribute your applications/components, be sure that the   
          deployment machine has these run-time libraries.                 
****************************************************************************
Trying to update options file: C:\Documents and Settings\Administrator\Application Data\MathWorks\MATLAB\R2008a\compopts.bat
From template:              C:\PROGRA~1\MATLAB\R2008a\bin\win32\mbuildopts\msvc90compp.bat
Done . . .

2. 输入deploytool命令,写一个你所需要生成的动态链接文件的名称a[不要与函数名称相重叠],在Target选择C shared library,确定,生成a.prj文件,在弹出的窗口中点击add Files,将你所需要编译转换的matlab函数文件添加进去[支持嵌套函数,struct],注意保证函数文件的正确性,否则后面容易出错。
C:\Program Files\MATLAB\R2008a\toolbox\matlab\imagesci\imread.m


3.点击build,生成srcdistrib两个文件夹,build过程中的信息最好保存为log文件,文件中的Link......里面有很多.lib,都是C运行程序时所要调用到的静态链接文件,在C文件中需添加进去。

4. 点击package,会自动将需要的文件打包为exe文件,里面包含a.dlla.ha.lib,最后运行时只需要这些文件就可以了,但是为了在工程中能看到编译后的原matlab函数在C中的定义,从而便于编程和调试还需添加src文件夹下的 _component_data.ca.c


Visual Studio 2008方面的操作
1. 建立一个C程序的工程
文件->新建->项目->Win32->Win32控制台应用程序->确定->下一步->选择控制台应用程序和空项目
2. b.dll拷贝至执行目录下(debug)
3. 若主机已安装matlab,则需添加路径,工具->选项


若主机未安装matlab,则需安装MCR - matlab compiler runtime,然后按上述要求添加路径,运行程序。
5.写一个test.c文件,如下
#include "matrix.h"
#include "b.h" 
#pragma comment(lib,"b.lib")
#pragma comment(lib,"mclmcrrt.lib")
int main(int argc, char* argv[])
    int nlhs=1;
    int nrhs=1;
    char *name="test.jpg";
    mxArray *plhs[1];
    mxArray *prhs[1];
   
    printf("Hello World! \n");
    bInitialize();
    prhs[0]=mxCreateString(name);
    mlxImread(nlhs,plhs,nrhs,prhs);   
    printf("Hello World 2! \n");
    bTerminate();printf输出格式matlab
    return 0;
}