3、一个例子
从这个示例中,我们看出引擎应用程序是如何编制的:
/* $Revision: 1.3 $ */
/*
* engdemo.c
*
* This is a simple program that illustrates how to call the
* MATLAB engine functions from a C program.
*
* Copyright (c) 1996-1998 The MathWorks, Inc.
* All rights reserved
*/
#include "engine.h"
#define BUFSIZE 256
int main()
{
Engine *ep;
mxArray *T = NULL, *result = NULL;
char buffer[BUFSIZE];
double time[10] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0,
8.0, 9.0 };
6-6
/*
* Start the MATLAB engine locally by executing the string
* "matlab".
*
* To start the session on a remote host, use the name of
* the host as the string rather than \0
*
* For more complicated cases, use any string with whitespace,
* and that string will be executed literally to start MATLAB.
*/
if (!(ep = engOpen("\0"))) {
fprintf(stderr, "\nCan't start MATLAB engine\n");
return EXIT_FAILURE;
} /*启动MATLAB引擎*/
/*
* PART I
*
* For the first half of this demonstration, we will send data
* to MATLAB, analyze the data, and plot the result.
*/
/*
/*
* Create a variable for our data.
*/
T = mxCreateDoubleMatrix(1, 10, mxREAL); /*创建一个矩阵*/
mxSetName(T, "T"); /*设置矩阵的名字为“T”*/
memcpy((void *)mxGetPr(T), (void *)time, sizeof(time)); /*向矩阵“T”赋值*/
/*
* 把矩阵“T”置入MATLAB引擎
*/
engPutArray(ep, T)
/*
* Evaluate a function of time, distance = (1/2)g.*t.^2
* (g is the acceleration due to gravity).
*/
engEvalString(ep, "D = .5.*(–9.8).*T.^2;"); /*执行MATLAB
命令:D = .5.*(–9.8).*T.^2;*/
/*
* 绘制图象.
*/
engEvalString(ep, "plot(T,D);"); /*执行MATLAB命令:绘图*/
engEvalString(ep, "title('Position vs. Time for a falling
object');"); /*执行MATLAB命令:给图象加标题*/
engEvalString(ep, "xlabel('Time (seconds)');"); /*执行MATLAB命令:设置X轴坐标*/
engEvalString(ep, "xlabel('Time (seconds)');"); /*执行MATLAB命令:设置X轴坐标*/
engEvalString(ep, "ylabel('Position (meters)');"); /*执行MATLAB命令:设置Y轴
坐标*/
/*
* Use fgetc() to make sure that we pause long enough to be
* able to see the plot.
*/
printf("Hit return to continue\n\n");
fgetc(stdin);
/*
* We're done for Part I! Free memory, close MATLAB engine.
*/
printf("Done for Part I.\n");
mxDestroyArray(T); /*从内存中撤销矩阵“T”*/
engEvalString(ep, "close;"); /*关闭刚才显示图象的窗口*/
/*
* PART II
*
* For the second half of this demonstration, we will request
* a MATLAB string, which should define a variable X. MATLAB
* will evaluate the string and create the variable. We
* will then recover the variable, and determine its type.
*/
*/
/*
* Use engOutputBuffer to capture MATLAB output, so we can
* echo it back.
*/
engOutputBuffer(ep, buffer, BUFSIZE); /*构建MATLAB文本输入缓冲区*/
while (result == NULL) {
char str[BUFSIZE];
/*
* Get a string input from the user.
*/
printf("Enter a MATLAB command to evaluate. This
command should\n");
printf("create a variable X. This program will then
determine\n");
printf("what kind of variable you created.\n");
printf("For example: X = 1:5\n");
printf(">> "); /*要求用户输入一个MATLAB命令*/
fgets(str, BUFSIZE–1, stdin); /*获得用户输入*/
/*
* Evaluate input with engEvalString.
*/
engEvalString(ep, str); /*执行用户输入的MATLAB命令*/
engEvalString(ep, str); /*执行用户输入的MATLAB命令*/
/*
* Echo the output from the command. First two characters
* are always the double prompt (>>).
*/
printf("%s", buffer+2); /*显示该MATLAB命令的执行情况*/
/*
* Get result of computation.
*/
printf("\nRetrieving X...\n");
if ((result = engGetArray(ep,"X")) == NULL) /*判断是否可以从MATLAB
引擎中获得矩阵“X”*/
printf("Oops! You didn't create a variable X.\n\n");
else
printf("X is class %s\t\n", mxGetClassName(result)); /*显示矩阵“X”
的类型*/
} /* while(result==NULL)*/
/*
* We're done! Free memory, close MATLAB engine and exit.
*/
printf("Done!\n");
mxDestroyArray(result); /*从内存中撤销矩阵“T”*/
engClose(ep); /*关闭MATLAB引擎*/
return EXIT_SUCCESS; /*返回*/
}
4、引擎应用程序的编译
对于象上例中的控制台程序,可以在MATLAB命令行中直接使用带-f参数的mex命令编译。
如果在普通win32 application中使用MATLAB引擎,情况则比较复杂。在Windows中,MATLAB引擎是通过ActiveX被调用的。因此你需要先Create一个OLE Automation Sever和一个OLE Client,然后通过OLE方式调用这个MATLAB引擎。具体做法可参阅相关MATLAB随机文档。
5、总结
MATLAB引擎的调用与其它引擎(例如数据库引擎)的调用很类似,其步骤是联接\启动引擎,然后向引擎发送命令,获得引擎处理结果。
结束语
上面简要介绍了MATLAB与C++的几种接口方式,我们可以根据要求的不同采用相应的方式。
此外,MATLAB还提供了一个数学库,由此数学库,我们可以获得对MATLAB内部命令更多的访问权和更灵活的访问方式。具体内容可参考MATLAB的相关随机文档。
·参考文献
1、MATLAB随机文档:apiguide.pdf
2、MATLAB随机文档:apiref.pdf
3、MATLAB随机文档:c_math_ref1.pdf
·致谢
感谢loverboy的鼓励,他声称要把这些帖子放到他的主页上,才使得我能够在短时间完成这篇接口问题的文章,西西。
·备注
mex编译器在VC6下的设置可参阅本板19#文
Bluesky的意见:
VC程序员最好用visual matcom。VB用matrixVB mathtool在主页上提供免费试用,快去下吧。matlab的功能可在你的VC,VB中实现,而且只需两个dll即可发行。
源文档 http://www.dytrol.com/dispbbs.asp?boardID=11&ID=258&page=5
[ 本帖最后由 ChaChing 于 2009-3-15 08:04 编辑 ] |