next up previous
Next: Tracer de polynôme (en Up: Outils pour l'ingénieur - Previous: Outils pour l'ingénieur -

Exemple d'un programme C utilisant le moteur MATLAB.

/* $Revision: 1.1 $ */
/*
 *	engdemo.c
 *
 *	This is a simple program that illustrates how to call the MATLAB
 *	Engine functions from a C program.
 *
 *      Copyright (c) 1996 The MathWorks, Inc.
 *      All rights reserved
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "engine.h"

void main()

{
	Engine *ep;
	mxArray *T = NULL, *result = NULL;
	char buffer[256], str[256];
	double time[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

	/*
	 * Start the MATLAB engine locally by executing the string
	 * "matlab"
	 *
	 * To start the session on a remote host, use the name of
	 * the host as the string rather than \0
	 *
	 * For more complicated cases, use any string with whitespace,
	 * and that string will be executed literally to start MATLAB
	 */
	if (!(ep = engOpen("\0"))) {
		fprintf(stderr, "\nCan't start MATLAB engine\n");
		exit(-1);
	}

	/*
	 * PART I
	 *
	 * For the first half of this demonstration, we will send data
	 * to MATLAB, analyze the data, and plot the result.
	 */

	/* 
	 * Create a variable from our data
	 */
	T = mxCreateDoubleMatrix(1, 10, mxREAL);
	mxSetName(T, "T");
	memcpy((char *) mxGetPr(T), (char *) time, 10*sizeof(double));

	/*
	 * Place the variable T into the MATLAB workspace
	 */
	engPutArray(ep, T);

	/*
	 * Evaluate a function of time, distance = (1/2)g.*t.^2
	 * (g is the acceleration due to gravity)
	 */
	engEvalString(ep, "D = .5.*(-9.8).*T.^2;");

	/*
	 * Plot the result
	 */
	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)');");

	/*
	 * use fgetc() to make sure that we pause long enough to be
	 * able to see the plot
	 */
	printf("Hit return to continue\n\n");
	fgetc(stdin);
	engEvalString(ep, "close;");

	/* la partie I suffie pour le TD. Il ne faut pas oublier
         * pour autant  de liberer la memoire et fermer MATLAB comme
         * c'est fait a la fin de ce programme 
         */

	/*
	 * PART II
	 *
	 * For the second half of this demonstration, we will request
	 * a MATLAB string, which should define a variable X.  MATLAB
	 * will evaluate the string and create the variable.  We
	 * will then recover the variable, and determine its type.
	 */
	  
	/*
	 * Use engOutputBuffer to capture MATLAB output, so we can
	 * echo it back.
	 */
	engOutputBuffer(ep, buffer, 256);
	
	while (result == NULL) {
	  /*
	   * Get a string input from the user
	   */
	  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, 255, stdin);
	  
	  /*
	   * Evaluate input with engEvalString
	   */
	  engEvalString(ep, str);
	  
	  /*
	   * Echo the output from the command.  First two characters are
	   * always the double prompt (>>).
	   */
	  printf("%s", buffer+2);
	  
	  /*
	   * Get result of computation
	   */
	  printf("\nRetrieving X...\n");
	  if ((result = engGetArray(ep,"X")) == NULL)
	    printf("Oops! You didn't create a variable X.\n\n");
	  else {
	    mxClassID category;
	    category = mxGetClassID(result);
	    switch(category) {
	    case mxCELL_CLASS :
	      printf("X is a cell array (class mxCELL_CLASS).\n");
	      break;
	    case mxSTRUCT_CLASS :
	      printf("X is a structure array (class mxSTRUCT_CLASS).\n");
	      break;
	    case mxOBJECT_CLASS :
	      printf("X is an object array (class mxOBJECT_CLASS).\n");
	      break;
	    case mxCHAR_CLASS :
	      printf("X is a character array (class mxCHAR_CLASS).\n");
	      break;
	    case mxSPARSE_CLASS :
	      printf("X is a sparse matrix (class mxSPARSE_CLASS).\n");
	      break;
	    case mxDOUBLE_CLASS :
	      printf("X is a double array (class mxDOUBLE_CLASS).\n");
	      break;
	    }
	  }
	}

	/*
	 * We're done! Free memory, close MATLAB engine and exit.
	 */
	printf("Done!\n");
	mxDestroyArray(T);
	mxDestroyArray(result);
	engClose(ep);
	
	exit(0);
}



Olivier Ricou
10/9/1997