主题:CBC与Matlab的混合编程!!
在Project\Option..\Directories/Conditionals设置了以下的路径;
● 在Include Path中,加入目录路径\extern\include,该路径包含了engine.h和matlab.h等有用的头文件。
● 在Library Path中,加入\bin和\extern\include。这两个目录路径包含了可外部调用的DLL和LIB文件。
并在Project\Add to Project中加入了_libeng.lib,_libmat.lib和_libmx.lib三个库文件
编写如下的代码:
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------- -----------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#include <Utilcls.h>
#include "math.h"
#include <engine.h>
TForm1 *Form1;
//---------------------------------------------------------- -----------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------- -----------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
const int N=50;
double x[N],y[N];
int j=1;
for(int i=0;i<N;i++)
{x=i+1;
y=sin(x)+j*log(x);
j*=-1;}
Engine *ep;
if(!(engOpen(NULL)))
{Application->MessageBox("不能打开Matlab","",MB_OK);
}
mxArray *xx=mxCreateDoubleMatrix(1,N,mxREAL);
mxArray *yy=mxCreateDoubleMatrix(1,N,mxREAL);
memcpy(mxGetPr(xx),x,N*sizeof(double));
memcpy(mxGetPr(yy),y,N*sizeof(double));
engPutVariable(ep,"xx",xx);
engPutVariable(ep,"yy",yy);
engEvalString(ep,"plot(xx,yy);");
mxDestroyArray(xx);
mxDestroyArray(yy);
engClose(ep);
}
可以编译过去,运行后,按Button1会出现下面的错误,请大虾们指教指教!!!!!
Project Project1.exe raised exception class EAccessViolation with message 'Access Violation' at address 100014E3 in module "LIBENG.DLL".
Read of Address 0000000E.Process stopped. Use Step or Run to continue.
● 在Include Path中,加入目录路径\extern\include,该路径包含了engine.h和matlab.h等有用的头文件。
● 在Library Path中,加入\bin和\extern\include。这两个目录路径包含了可外部调用的DLL和LIB文件。
并在Project\Add to Project中加入了_libeng.lib,_libmat.lib和_libmx.lib三个库文件
编写如下的代码:
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------- -----------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#include <Utilcls.h>
#include "math.h"
#include <engine.h>
TForm1 *Form1;
//---------------------------------------------------------- -----------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------- -----------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
const int N=50;
double x[N],y[N];
int j=1;
for(int i=0;i<N;i++)
{x=i+1;
y=sin(x)+j*log(x);
j*=-1;}
Engine *ep;
if(!(engOpen(NULL)))
{Application->MessageBox("不能打开Matlab","",MB_OK);
}
mxArray *xx=mxCreateDoubleMatrix(1,N,mxREAL);
mxArray *yy=mxCreateDoubleMatrix(1,N,mxREAL);
memcpy(mxGetPr(xx),x,N*sizeof(double));
memcpy(mxGetPr(yy),y,N*sizeof(double));
engPutVariable(ep,"xx",xx);
engPutVariable(ep,"yy",yy);
engEvalString(ep,"plot(xx,yy);");
mxDestroyArray(xx);
mxDestroyArray(yy);
engClose(ep);
}
可以编译过去,运行后,按Button1会出现下面的错误,请大虾们指教指教!!!!!
Project Project1.exe raised exception class EAccessViolation with message 'Access Violation' at address 100014E3 in module "LIBENG.DLL".
Read of Address 0000000E.Process stopped. Use Step or Run to continue.