C++调用MATLAB引擎
我说要有光2009/05/19软件综合 IP:河北
这是MATLAB自带的例子,我在WindowsXP(SP2), MATLAB R2007b, VC6.0下实现。
具体的步骤是:
1). 设置系统环境变量path,path里面要包括MATLAB的exe, dll那个路径
%MATLAB_PATH%\bin\win32
只要MATLAB是正确安装,它应该会自动配置path环境变量的。
右击我的电脑->属性->高级->环境变量->系统变量
双击path
path值应该有以下两个值,中间是分号
d:\MATLABR2007b\bin;d:\MATLABR2007b\bin\win32
2). 设置VC的路径
运行VC,转到options
include增加MATLAB的include路径 %MATLAB_PATH%\extern\include
lib增加%MATLAB_PATH%\extern\lib\win32\microsoft
这一步以后都不用做了。
3). 在VC中新建一个console项目(也可以是win32,mfc等其他项目),项目的link输入增加XXXXXXXXXbXXXXXXXXb(每个工程都要添加,否则编译不能通过)
4). main函数的代码:
/* $Revision: 1.8.4.1 $ */
/*
* engdemo.c
*
* This is a simple program that illustrates how to call the MATLAB
* Engine functions from a C program.
*
* Copyright 1984-2003 The MathWorks, Inc.
* All rights reserved
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "engine.h"
#define BUFSIZE 256

int main()
{
Engine *ep;
mxArray *T = NULL, *result = NULL;
char buffer[BUFSIZE+1];
double time[10] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };

/*
* 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;
}

/*
* 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);
memcpy((void *)mxGetPr(T), (void *)time, sizeof(time));
/*
* Place the variable T into the MATLAB workspace
*/
engPutVariable(ep, "T", 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;");

/*
* Plot the result
*/
engEvalString(ep, "plot(T,D);");
engEvalString(ep, "title('[s:9]osition vs. Time for a falling object');");
engEvalString(ep, "xlabel('Time (seconds)');");
engEvalString(ep, "ylabel('[s:9]osition (meters)');");

/*
* 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);
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. Ensure first that the buffer is always NULL
* terminated.
*/

buffer[BUFSIZE] = '\0';
engOutputBuffer(ep, buffer, BUFSIZE);
while (result == NULL) {
     char str[BUFSIZE+1];
     /*
      * 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(">> ");

     fgets(str, BUFSIZE, stdin);
  
     /*
      * Evaluate input with engEvalString
      */
     engEvalString(ep, str);
    
     /*
      * Echo the output from the command. First two characters are
      * always the double prompt (>>).
      */
     printf("%s", buffer+2);
    
     /*
      * Get result of computation
      */
     printf("\nRetrieving X...\n");
     if ((result = engGetVariable(ep,"X")) == NULL)
       printf("Oops! You didn't create a variable X.\n\n");
     else {
   printf("X is class %s\t\n", mxGetClassName(result));
     }
}

/*
* We're done! Free memory, close MATLAB engine and exit.
*/
printf("Done!\n");
mxDestroyArray(result);
engClose(ep);

return EXIT_SUCCESS;
}

编译运行,等一会儿会有一个MATLAB图形窗口弹出。

4364f523203197409922edfd.jpg
来自:计算机科学 / 软件综合
5
已屏蔽 原因:{{ notice.reason }}已屏蔽
{{notice.noticeContent}}
~~空空如也
我说要有光 作者
15年2个月前 IP:未同步
101473
引用第1楼dctyu于2009-05-20 16:41发表的  :
LZ真是太强了。我见过的,大批大批的这方面专业的大学生不会这不会那。

不要误会。。。 这个是我转载来的 = =

我只是会用而已 还没强大到写教程的程度。。。
引用
评论
加载评论中,请稍候...
200字以内,仅用于支线交流,主线讨论请采用回复功能。
折叠评论

想参与大家的讨论?现在就 登录 或者 注册

所属专业
上级专业
同级专业
我说要有光
进士 学者 机友 笔友
文章
317
回复
5531
学术分
15
2008/03/06注册,11时43分前活动

Inspiration, Innovation, Discovery

主体类型:个人
所属领域:无
认证方式:手机号
IP归属地:未同步
文件下载
加载中...
{{errorInfo}}
{{downloadWarning}}
你在 {{downloadTime}} 下载过当前文件。
文件名称:{{resource.defaultFile.name}}
下载次数:{{resource.hits}}
上传用户:{{uploader.username}}
所需积分:{{costScores}},{{holdScores}}下载当前附件免费{{description}}
积分不足,去充值
文件已丢失

当前账号的附件下载数量限制如下:
时段 个数
{{f.startingTime}}点 - {{f.endTime}}点 {{f.fileCount}}
视频暂不能访问,请登录试试
仅供内部学术交流或培训使用,请先保存到本地。本内容不代表科创观点,未经原作者同意,请勿转载。
音频暂不能访问,请登录试试
支持的图片格式:jpg, jpeg, png
插入公式
评论控制
加载中...
文号:{{pid}}
投诉或举报
加载中...
{{tip}}
请选择违规类型:
{{reason.type}}

空空如也

加载中...
详情
详情
推送到专栏从专栏移除
设为匿名取消匿名
查看作者
回复
只看作者
加入收藏取消收藏
收藏
取消收藏
折叠回复
置顶取消置顶
评学术分
鼓励
设为精选取消精选
管理提醒
编辑
通过审核
评论控制
退修或删除
历史版本
违规记录
投诉或举报
加入黑名单移除黑名单
查看IP
{{format('YYYY/MM/DD HH:mm:ss', toc)}}