next up previous
Next: Compilation Up: MATLAB dans un programme Previous: MATLAB dans un programme

Exemple d'un programme C appelant MATLAB :

Fait calculer par MATLAB les valeurs propres de $A\, A^T$.

#include <stdio.h>
#include "engine.h"
static double Areal[6]={1, 2, 3, 4, 5, 6}

void main()
{
  Engine *ep;
  mxArray *a, *d;
  double *Dreal, *Dimag;
  a = mxCreateDoubleMatrix(3, 2, mxREAL);

  memcpy(mxGetPr(a), Areal, 6*sizeof(double));
  mxSetName(a, "A");
  if !(ep = engOpen("\0"))  /* \0 ou adresse IP */
  Printf("Can't start up MATLAB engine.");
  engPutArray(ep, a);
  engEvalString(ep, "d=eig(A*A')");
  d = engGetArray(ep, "d");
  engClose(ep);
  Dreal = mxGetPr(d);
  Dimag = mxGetPi(d);
  if(Dimag)
  Printf("Eigval 2: %g+%gi\n", Dreal[1], Dimag[1]);
  else
  Printf("Eigval 2: %g\n", Dreal[1]);
  mxDestroyArray(a);
  mxDestroyArray(d);
}



Olivier Ricou
10/6/1997