⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mymatlabengin.cpp

📁 matlab 和c或者c++ 接口程序
💻 CPP
字号:
#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]={1.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"
//
if(!(ep=engOpen("\0")))
{fprintf(stderr,"can not start matlab engine \n");
 return -1;
}
T=mxCreateDoubleMatrix(1,10,mxREAL);
memcpy((void *)mxGetPr(T),(void *)time,sizeof(time));
engPutVariable(ep,"T",T);

engEvalString(ep,"D= .5.*(-9.8).*T.^2;");
//////////////////////////////////////////////////////////////////////////
//plot result
engEvalString(ep,"plot(T,D);");
engEvalString(ep,"title('position vs time for a falling object');");
printf("hit return to continue\n");
fgetc(stdin);
mxDestroyArray(T);
engEvalString(ep,"close;");
buffer[BUFSIZE]='\0';
engOutputBuffer(ep,buffer,BUFSIZE);
while (result==NULL) {
	char str[BUFSIZE+1];
       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);
		engEvalString(ep,str);
         /*
	     * Echo the output from the command.  First two characters are
	     * always the double prompt (>>).  first 2 characters are >>
	     */
	    printf("%s", buffer+2);
		printf("\nRetrieving X...\n");
		if((result=engGetVariable(ep,"X"))==NULL)
			printf("you did not create a variable X \n");
		else
            printf("X is class %s\t\n",mxGetClassName(result));

}

    printf("Done!\n");
	mxDestroyArray(result);
    engClose(ep);
	return 1;
}

⌨️ 快捷键说明

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