|
// 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.
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