Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

MATLAB Engine API

, 1 Jul 2003
Using MATLAB Engine API to control MATLAB
matlabeng_demo.zip
MatlabClass.plg
MatlabClass.dsp
MatlabClass.dsw
MatlabClass.ncb
MatlabClass.opt
matlabeng_src.zip
// 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

Share

About the Author

A. Riazi
CEO Misbah3Com
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
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 Acqusition 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 competetion, my articles are:

You can see list of my articles, by clicking here


| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150326.1 | Last Updated 2 Jul 2003
Article Copyright 2003 by A. Riazi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid