unit1.cpp

来自「调用matlab详细程序 调用matlab详细程序」· C++ 代码 · 共 53 行

CPP
53
字号
//---------------------------------------------------------------------------

#include <vcl.h>
#include "engine.h" //matlab->extern->include 中有,在projects->options添加include 头文件搜索路径即可
#define  BUFSIZE 256
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
Engine *ep;
bool vis;
mxArray *T = NULL, *result = NULL;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  double time[10] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };

	T = mxCreateDoubleMatrix(1, 10, mxREAL);
	memcpy((unsigned char*)mxGetPr(T),time, 10*sizeof(double));
       	engPutVariable(ep, "T", T);
	engEvalString(ep, "D = .5.*(-9.8).*T.^2;");
	engEvalString(ep, "plot(T,D);");
        //engEvalString(ep,"set(gcf,'Visible','off')") ;
	engEvalString(ep, "title('Position vs. Time for a falling object');");
	engEvalString(ep, "xlabel('Time (seconds)');");
	engEvalString(ep, "ylabel('Position (meters)');");
        engEvalString(ep,"saveas(gcf,'2.bmp')") ;
        Form1->Image1->Picture->LoadFromFile("C:\\MATLAB7\\2.bmp");
	mxDestroyArray(result);
	engClose(ep);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormActivate(TObject *Sender)
{
if (!(ep = engOpen("\0"))) {
		ShowMessage("Can't Open The Engine!");

	}
        engSetVisible(ep, 0);

}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?