Click here to Skip to main content
15,891,136 members
Articles / Desktop Programming / MFC

MATLAB Engine API

Rate me:
Please Sign up or sign in to vote.
4.91/5 (48 votes)
1 Jul 20033 min read 1.1M   9.1K   93  
Using MATLAB Engine API to control MATLAB
// MatlabClass.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "MatlabEng.h"

#define message(x) printf(x"\n\n")

int main(int argc, char* argv[])
{
	CMatlabEng matlab;
	
	//open new matlab session
	message("Starting MATLAB");
	matlab.Open(NULL);

	message("Hiding MATLAB");
	matlab.SetVisible(FALSE);
	message("Press any key to continue");
	getch();

	message("Showing MATLAB");
	matlab.SetVisible(TRUE);
	message("Press any key to continue");
	getch();
	
	mxArray *T = NULL;
	double time[10] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };

	//create matrix
	T = mxCreateDoubleMatrix(1, 10, mxREAL);
	memcpy((void *)mxGetPr(T), (void *)time, sizeof(time));
	
	//send matrix T to matlab
	message("Send matrix T to matlab");
	matlab.PutVariable("T", T);

	//Evaluate matlab command
	matlab.EvalString("D = .5.*(-9.8).*T.^2;");

	//Plot results
	message("Plot(T, D)");
	matlab.EvalString("plot(T,D);");	
	matlab.EvalString("title('Position vs. Time for a falling object');");
	matlab.EvalString("xlabel('Time (seconds)');");
	matlab.EvalString("ylabel('Position (meters)');");
	matlab.EvalString("grid;");

	//pause to see results
	message("Press any key to continue");
	getch();
	
	//destroy matrix
	mxDestroyArray(T);
	
	//show MATLAB graphics capabilities
	message("Showing MATLAB graphics capabilities");
	matlab.EvalString("z=peaks(25);");
	matlab.EvalString("surf(z);");
	matlab.EvalString("colormap(jet);");
	matlab.EvalString("knot;");

	//pause
	getch();

	//close session
	matlab.Close();

	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
CEO Solaris Electronics LLC
United Arab Emirates United Arab Emirates
I was born in Shiraz, a very beautiful famous city in Iran. I started programming when I was 12 years old with GWBASIC. Since now, I worked with various programming languages from Basic, Foxpro, C/C++, Visual Basic, Pascal to MATLAB and now Visual C++.
I graduated from Iran University of Science & Technology in Communication Eng., and now work as a system programmer for a telecommunication industry.
I wrote several programs and drivers for Synthesizers, Power Amplifiers, GPIB, GPS devices, Radio cards, Data Acquisition cards and so many related devices.
I'm author of several books like Learning C (primary and advanced), Learning Visual Basic, API application for VB, Teach Yourself Object Oriented Programming (OOP) and etc.
I'm winner of January, May, August 2003 and April 2005 best article of month competition, my articles are:


You can see list of my articles, by clicking here


Comments and Discussions