Hi, Here is a message that was sent to me a year ago. I hope it may help people trying to interface Matlab and ROOT. Cheers Damir Début du message réexpédié : > De: steve udriot <steve.udriot@cern.ch> > Date: Lun 1 juil 2002 17:48:39 Europe/Zurich > À: Damir Buskulic <buskulic@lapp.in2p3.fr> > Cc: Rene Brun <Rene.Brun@cern.ch> > Objet: Rép : [ROOT] Interface with Matlab ? > > You can try this... > > This is not convenient because it does only provide a "string" > interface > but it is maybe a start point... > Try the engdemo() method... > > All the best, > > Steve > / > *********************************************************************** > * > * MatlabAPI : written by Steve Udriot (13.02.02) > * > * > * > * This singleton class is an interface to Matlab external functions > * > * called eng_______ from the ${MATLAB}/extern/include/engine.h header > * > * and mx_______ from the ${MATLAB}/extern/include/engine.h header. > * > * > * > * One needs to load ${MATLAB}/extern/lib/glnx86/libeng.so. > * > * > * > * It starts a so-called Matlab engine at instantiation for once > * > * and closes it when deleted. > * > * > * > * For use on an other machine : > * > * MatlabAPI::Instance("ssh login@hostname matlab"); > * > > *********************************************************************** > */ > > > #ifndef MatlabAPI_h > #define MatlabAPI_h > > #if !defined (__CINT__) || defined (__MAKECINT__) > #include "Rtypes.h" > #endif > > ///////////////////////////////////////////////////////////////////// > // Forward Declarations // > ///////////////////////////////////////////////////////////////////// > > typedef struct engine Engine; > typedef struct mxArray_tag mxArray; > typedef enum { > mxREAL, > mxCOMPLEX > } mxComplexity; > typedef enum { > mxUNKNOWN_CLASS = 0, > mxCELL_CLASS, > mxSTRUCT_CLASS, > mxOBJECT_CLASS, > mxCHAR_CLASS, > mxSPARSE_CLASS, > mxDOUBLE_CLASS, > mxSINGLE_CLASS, > mxINT8_CLASS, > mxUINT8_CLASS, > mxINT16_CLASS, > mxUINT16_CLASS, > mxINT32_CLASS, > mxUINT32_CLASS, > mxINT64_CLASS, > mxUINT64_CLASS, > mxFUNCTION_CLASS, > mxOPAQUE_CLASS > } mxClassID; > > > > class MatlabAPI { > > ///////////////////////////////////////////////////////////////////// > // Singleton Initialization methods // > ///////////////////////////////////////////////////////////////////// > > private: > static MatlabAPI* _instance; //! static instance for MatlabAPI > > protected: > MatlabAPI(char *startcmd); > > public: > static MatlabAPI* Instance(char *startcmd = 0); > virtual ~MatlabAPI(); > > ///////////////////////////////////////////////////////////////////// > // Matlab methods // > ///////////////////////////////////////////////////////////////////// > private: > Engine *_engAPI; //! instance for Matlab engine > > public: > > Engine* engAPI() { return _engAPI; }; > > /**************************************************************** > * Matlab Engine * > ****************************************************************/ > protected: > static Engine* engOpen(const char *startcmd); > static int engClose(Engine *ep); > public: > static int engPutArray(Engine *ep,const mxArray *ap); > static int engEvalString(Engine *ep,const char *string); > static mxArray* engGetArray(Engine *ep, const char *name); > static int engOutputBuffer(Engine *ep, char *buffer, int buflen); > > /**************************************************************** > * Matlab Matrix * > ****************************************************************/ > static mxArray* mxCreateDoubleMatrix(int m, int n, mxComplexity > flag); > static void mxDestroyArray(mxArray *pa); > static void mxSetName(mxArray *pa, const char *s); > static double* mxGetPr(const mxArray *pa); > static mxClassID mxGetClassID(const mxArray *pa); > static const char* mxGetClassName(const mxArray *pa); > > > ///////////////////////////////////////////////////////////////////// > // Matlab Interface Tests : see URL // > // http://www.mathworks.com/access/helpdesk/help/techdoc // > // /matlab_external/ch06eng4.shtml#25603 // > ///////////////////////////////////////////////////////////////////// > void engdemo(); > > ClassDef(MatlabAPI,0) > }; > > #endif > > #include "MatlabAPI.h" > > ClassImp(MatlabAPI) > > ///////////////////////////////////////////////////////////////////// > // Singleton Initialization methods // > ///////////////////////////////////////////////////////////////////// > > MatlabAPI* MatlabAPI::_instance = 0; > > MatlabAPI* MatlabAPI::Instance(char *startcmd) { > > if (_instance == 0) > _instance = new MatlabAPI(startcmd); > > return _instance; > } > > MatlabAPI::MatlabAPI(char *startcmd) : > _engAPI(0) > { > if (_engAPI == 0) > if (!(_engAPI = MatlabAPI::engOpen(startcmd))) { > printf("MatlabAPI::MatlabAPI() : Can't start MATLAB engine\n"); > return; > } > } > > MatlabAPI::~MatlabAPI() { > > _instance = 0; > > if (_engAPI) > engClose(_engAPI); > } > > ///////////////////////////////////////////////////////////////////// > // Matlab Interface NameSpace // > ///////////////////////////////////////////////////////////////////// > > namespace Matlab { > extern "C" { > > #define type_of_call > > /**************************************************************** > * Matlab Engine * > ****************************************************************/ > extern Engine* type_of_call engOpen(const char *startcmd); > extern int type_of_call engClose(Engine *ep); > extern int type_of_call engPutArray(Engine *ep,const mxArray *ap); > extern int type_of_call engEvalString(Engine *ep,const char > *string); > extern mxArray* type_of_call engGetArray(Engine *ep, const char > *name); > extern int type_of_call engOutputBuffer(Engine *ep, char *buffer, > int buflen ); > > /**************************************************************** > * Matlab Matrix * > ****************************************************************/ > extern mxArray* type_of_call mxCreateDoubleMatrix(int m, int n, > mxComplexity flag); > extern void type_of_call mxDestroyArray(mxArray *pa); > extern void type_of_call mxSetName(mxArray *pa, const char *s); > extern double* type_of_call mxGetPr(const mxArray *pa); > extern mxClassID type_of_call mxGetClassID(const mxArray *pa); > extern const char* type_of_call mxGetClassName(const mxArray *pa); > > } > } > > > ///////////////////////////////////////////////////////////////////// > // Matlab Interface Methods // > ///////////////////////////////////////////////////////////////////// > > /******************************************************************** > * Matlab Engine * > ********************************************************************/ > > Engine* MatlabAPI::engOpen(const char *startcmd) > { > return Matlab::engOpen(startcmd); > } > > int MatlabAPI::engClose(Engine *ep) > { > return Matlab::engClose(ep); > } > > int MatlabAPI::engPutArray(Engine *ep,const mxArray *ap) > { > return Matlab::engPutArray(ep,ap); > } > > int MatlabAPI::engEvalString(Engine *ep,const char *string) > { > return Matlab::engEvalString(ep, string); > } > > mxArray* MatlabAPI::engGetArray(Engine *ep, const char *name) > { > return Matlab::engGetArray(ep, name); > } > > int MatlabAPI::engOutputBuffer(Engine *ep, char *buffer, int buflen) > { > return Matlab::engOutputBuffer(ep, buffer, buflen); > } > > /******************************************************************** > * Matlab Matrix * > ********************************************************************/ > mxArray* MatlabAPI::mxCreateDoubleMatrix(int m, int n, mxComplexity > flag) > { > return Matlab::mxCreateDoubleMatrix(m,n,flag); > } > > void MatlabAPI::mxDestroyArray(mxArray *pa) > { > return Matlab::mxDestroyArray(pa); > } > > void MatlabAPI::mxSetName(mxArray *pa, const char *s) > { > return Matlab::mxSetName(pa,s); > } > > double* MatlabAPI::mxGetPr(const mxArray *pa) > { > return Matlab::mxGetPr(pa); > } > > mxClassID MatlabAPI::mxGetClassID(const mxArray *pa) > { > return Matlab::mxGetClassID(pa); > } > > const char* MatlabAPI::mxGetClassName(const mxArray *pa) > { > return Matlab::mxGetClassName(pa); > } > > ///////////////////////////////////////////////////////////////////// > // Matlab Interface Demo : provided in Web Tutorial at URL // > // http://www.mathworks.com/access/helpdesk/help/techdoc // > // /matlab_external/ch06eng4.shtml#25603 // > ///////////////////////////////////////////////////////////////////// > > #include <stdlib.h> > #include <stdio.h> > #include <iomanip.h> > #include <string.h> > void MatlabAPI::engdemo() { > Engine *ep = _engAPI; > mxArray *T = NULL, *result = NULL; > static int BUFSIZE = 100; > char buffer[BUFSIZE]; > double time[10] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, > 8.0, 9.0 }; > /* > * PART I > * > */ > T = mxCreateDoubleMatrix(1, 10, mxREAL); > mxSetName(T, "T"); > memcpy((void *)mxGetPr(T), (void *)time, sizeof(time)); > engPutArray(ep, T); > engEvalString(ep, "D = .5.*(-9.8).*T.^2;"); > 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)');"); > printf("Press Return to continue\n\n"); > fgetc(stdin); > mxDestroyArray(T); > engEvalString(ep, "close;"); > /* > * PART II > * > */ > engOutputBuffer(ep, buffer, BUFSIZE); > while (result == NULL) { > char str[BUFSIZE]; > 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, BUFSIZE-1, stdin); > engEvalString(ep, str); > printf("%s", buffer+2); > printf("\nRetrieving X...\n"); > if ((result = engGetArray(ep,"X")) == NULL) > printf("Oops! You didn't create a variable X.\n\n"); > else { > printf("X is class %s\t\n", mxGetClassName(result)); > } > } > printf("Done!\n"); > mxDestroyArray(result); > } > #ifdef __CINT__ > > #pragma link off all globals; > #pragma link off all classes; > #pragma link off all functions; > > #pragma link C++ class MatlabAPI; > #pragma link C++ enum mxComplexity; > #pragma link C++ enum mxClassID; > > #endif > ==================================== Damir Buskulic, Universite de Savoie/LAPP Chemin de Bellevue, B.P. 110, F-74941 Annecy-le-Vieux Cedex, FRANCE Tel : +33 (0)450091600 e-mail: buskulic@lapp.in2p3.fr ====================================
This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:15 MET