Your SlideShare is downloading. ×
Integrating C, C++ with MATLAB<br />
Why integrate C/C++ with MATLAB?<br />Speed:<br />C, C++ code can provide really fast computation compared to MATLAB.<br /...
Why integrate C/C++ with MATLAB?<br />Data Structure Management<br />Speed<br />
Integrating C++ with MATLAB<br />For integration of C/C++ with MATLAB, we make use of MEX files<br />MEX files are pre-com...
MEX files<br />
Components of a MEX file<br />Every MEX file consists of two parts:<br />A gateway routine that interacts with Matlab proc...
Writing MEX files<br />It is always assumed when writing C programs that the program will start execution from the main()....
Writing MEX files<br />In order to make a mex-function, you must include the &quot;mex.h&quot; library.<br />#include &quo...
Writing MEX files<br />There are four input parameters to the mexFunction:<br />nlhs (Type = int)<br />plhs (Type = array ...
Writing MEX files<br />nlhs  represents the number of &quot;left hand side&quot; arguments.<br />plhs  is the actual outpu...
Writing MEX files<br />mxArrayis the main MATLAB structure used for holding data in MEX-Files is the. This structure can h...
Calling Built-In Functions from a MEX-File<br />MATLAB has provided the capability of calling pre-written functions in MAT...
Calling Built-In Functions from a MEX-File<br />Example:<br />mxArray *result;<br />mxArray *arguments[2];<br />arguments[...
Compiling a MEX file<br />In the MATLAB command prompt:,<br />Change the current directory to the location of the MEX sour...
Using the MEX binary<br />After successful compilation of the MEX source file, MATLAB produces the actual MEX binary that ...
Using the MEX binary<br />On different systems, the MEX binary goes by different extensions:<br />Windows=.dll<br />MacOSX...
Upcoming SlideShare
Loading in...5
×

C,C++ In Matlab

9,579

Published on

C,C++ In Matlab

Published in: Technology

Transcript of "C,C++ In Matlab"

  1. 1. Integrating C, C++ with MATLAB<br />
  2. 2. Why integrate C/C++ with MATLAB?<br />Speed:<br />C, C++ code can provide really fast computation compared to MATLAB.<br />Data structure management can be a difficult task in C,C++ but very easy in MATLAB.<br />
  3. 3. Why integrate C/C++ with MATLAB?<br />Data Structure Management<br />Speed<br />
  4. 4. Integrating C++ with MATLAB<br />For integration of C/C++ with MATLAB, we make use of MEX files<br />MEX files are pre-compiled files that are called from MATLAB<br />MEX files can be compiled from Matlab.m files<br />MEX files can also be compiled from external C/C++ code.<br />
  5. 5. MEX files<br />
  6. 6. Components of a MEX file<br />Every MEX file consists of two parts:<br />A gateway routine that interacts with Matlab procedures. Must be called ‘mexFunction()’. This is the point for the C++ code to enter. <br />Subroutine Section which contains any number of subroutines that are called from the gateway routine. Most portion of our computation will be inside these subroutines.<br />
  7. 7. Writing MEX files<br />It is always assumed when writing C programs that the program will start execution from the main(). On the contrary, MEX -Files always start execution from a special function called the mexFunction(). This function has return type void and is the &quot;gateway&quot; between the MATLAB function call, and our C code.<br />
  8. 8. Writing MEX files<br />In order to make a mex-function, you must include the &quot;mex.h&quot; library.<br />#include &quot;math.h&quot; <br />#include &quot;mex.h&quot;<br />
  9. 9. Writing MEX files<br />There are four input parameters to the mexFunction:<br />nlhs (Type = int)<br />plhs (Type = array of pointers to mxArrays)<br />nrhs (Type = int)<br />prhs (Type = const array of pointers to mxArrays)<br />
  10. 10. Writing MEX files<br />nlhs  represents the number of &quot;left hand side&quot; arguments.<br />plhs  is the actual output arguments. An mxArray is MATLAB&apos;s structure for holding data and each element in plhs holds an mxArray of data.<br />nrhs  holds the number of &quot;right hand side&quot; arguments.<br />prhs  holds all of the pointers to the mxArrays of input data<br />
  11. 11. Writing MEX files<br />mxArrayis the main MATLAB structure used for holding data in MEX-Files is the. This structure can hold real data, complex data, arrays, matrices, sparse-arrays, strings, and a whole host of other MATLAB data-structures.<br />
  12. 12. Calling Built-In Functions from a MEX-File<br />MATLAB has provided the capability of calling pre-written functions in MATLAB. Built-In functions of MATLAB have a parameter list similar to the mexFunction() itself.<br />
  13. 13. Calling Built-In Functions from a MEX-File<br />Example:<br />mxArray *result;<br />mxArray *arguments[2];<br />arguments[0] = mxCreateDoubleMatrix(1, 20, mxREAL);<br />arguments[1] = mxCreateDoubleMatrix(1, 10, mxREAL);<br />mexCallMATLAB(1,&result,2,arguments,&quot;conv&quot;);<br />
  14. 14. Compiling a MEX file<br />In the MATLAB command prompt:,<br />Change the current directory to the location of the MEX source file. <br />Type: mexfilename.c into the MATLAB command window.<br />MATLAB may ask you to choose a compiler. Choose the compiler with MATLAB in its directory path. <br />The new function will be called with the same name as our file. <br />
  15. 15. Using the MEX binary<br />After successful compilation of the MEX source file, MATLAB produces the actual MEX binary that can be called as a normal MATLAB function. To call this function, we must be in the same directory as the binary. <br />
  16. 16. Using the MEX binary<br />On different systems, the MEX binary goes by different extensions:<br />Windows=.dll<br />MacOSX=.mexmac<br />Solaris=.mexsol<br />Linux=.mexlx<br />

×