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

📄 runmatlab.cpp

📁 斯坦福Energy211/CME211课《c++编程——地球科学科学家和工程师》的课件
💻 CPP
字号:
#include <iostream>#include "engine.h"#define  BUFSIZE 256using namespace std;int main(){   	// Launch MATLAB	Engine *ep = engOpen( "" );	if ( ep == NULL ) {		cerr << "\nCan't start MATLAB engine\n";		return EXIT_FAILURE;	}	// Create new MATLAB vector	mxArray *T = mxCreateDoubleMatrix( 1, 10, mxREAL );	double *p = mxGetPr( T );	// Store values in it, through pointer p	double time[10] = { 0.0, 1.0, 2.0, 3.0, 						4.0, 5.0, 6.0, 7.0, 						8.0, 9.0 };	for ( int i = 0; i < 10; i++ )		p[i] = time[i];	// Put the vector in the MATLAB workspace	engPutVariable( ep, "T", T );	// Now work with it in MATLAB	engEvalString( ep, "D = .5.*(-9.8).*T.^2;" );	//engEvalString( ep, "plot(T,D);" );	//engEvalString( ep, "title('Position vs. Time for a falling object');" );	//engEvalString( ep, "xlabel('Time (seconds)');" );	//engEvalString( ep, "ylabel('Position (meters)');" );	cout << "Hit return to continue\n\n";	string s;	getline( cin, s );	cout << "Done for Part I.\n";	mxDestroyArray( T );	// This closes the figure window	engEvalString( ep, "close;" );	char buffer[BUFSIZE];	// Cause MATLAB command window output to be stored	// in buffer, up to BUFSIZE characters	engOutputBuffer( ep, buffer, BUFSIZE );	mxArray *result = NULL;	while ( result == NULL ) {		cout << "Enter a MATLAB command to evaluate.  This command should\n";		cout << "create a variable X.  This program will then determine\n";		cout << "what kind of variable you created.\n";		cout << "For example: X = 1:5\n";		cout << ">> ";		// read command		string cmd;		getline( cin, cmd );	  		// evaluate command in MATLAB workspace		engEvalString( ep, cmd.c_str() );	    		// write output, if any		cout << buffer;		// Now look for X in workspace		cout << "\nRetrieving X...\n";		if ( ( result = engGetVariable( ep, "X" ) ) == NULL)			cout << "Oops! You didn't create a variable X.\n\n";		else			cout << "X is of type " << mxGetClassName( result ) << endl;	}	// cleanup	cout << "Done!\n";	mxDestroyArray( result );	engClose( ep );   	return EXIT_SUCCESS;}

⌨️ 快捷键说明

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