Tutorial 2 : Writing Engine scripts.
After having set Visual Studio to be integrated with MATLAB, you need to add some fragments of code along with your C/C++ program to call or send values to MATLAB.
Everything you need will be contained in the libraries engine.h and matrix.h
Every engine application will have a pointer of data type Engine that defines an engine session. We will be using variable addresses to copy values using pointers and using in either MATLAB or C or both.
Opening and closing engine sessions :
Following functions will be needed for its working :
- ep = engOpen() - where engine session starts. ep of type for MATLAB Engine.
- engClose(ep) - closes engine session defined by pointer ep
Pointers to store values for transferring between MATLAB and C :
Remember, for transferring arrays and values with MATLAB, we need to have an intermediate data-type, mxArray. Creates MATLAB arrays.
Define pointers of mxArray data-type as an array using, mxCreateDoubleMatrix, mxCreateNumericMatrix, etc. (See documentation for further types and its arguments)
For example, mxArray *array = mxCreateDoubleMatrix(5,3,mxREAL);
creates a pointer to a double matrix with 5 rows and 3 columns and has elements belonging to Real Numbers.
Performing the actual putting,getting and evaluation of values in MATLAB workspace :
Following functions might be used to get or put value in mxArray variables from/into MATLAB workspace :
- engEvalString(ep,"MATLAB code") - Puts whatever written in string to MATLAB workspace opened by ep Engine session pointer.
- engPutVariable(ep,"MATLAB variable",mxArray pointer) - Puts values from an address in C space to an address in MATLAB workspace.
- mxArray pointer = engGetVariable(ep,"MATLAB variable") - Gets values from MATLAB workspace variable specified in " " and is stored in mxArray pointers.
- mxGetPr(mxArray pointer) - Gets the starting address of the first real element of pointer.
memcpy(address of destination,address of source,number of bytes to be copied)
Use (void *) , also called General Purpose Pointer as it can be used as any data-type pointer.
Moreover, along with engOpen you can add error messages with it.(Refer Documentation of MATLAB)
engwindemo.c is a good example to learn from. Below will be the screenshots of my code, describing stages of Engine session. For full code, you can visit my github repo HERE
No comments:
Post a Comment