Your SlideShare is downloading. ×
C,C++ In Matlab
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

C,C++ In Matlab


Published on

C,C++ In Matlab

C,C++ In Matlab

Published in: Technology

  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Integrating C, C++ with MATLAB
  • 2. Why integrate C/C++ with MATLAB?
    C, C++ code can provide really fast computation compared to MATLAB.
    Data structure management can be a difficult task in C,C++ but very easy in MATLAB.
  • 3. Why integrate C/C++ with MATLAB?
    Data Structure Management
  • 4. Integrating C++ with MATLAB
    For integration of C/C++ with MATLAB, we make use of MEX files
    MEX files are pre-compiled files that are called from MATLAB
    MEX files can be compiled from Matlab.m files
    MEX files can also be compiled from external C/C++ code.
  • 5. MEX files
  • 6. Components of a MEX file
    Every MEX file consists of two parts:
    A gateway routine that interacts with Matlab procedures. Must be called ‘mexFunction()’. This is the point for the C++ code to enter.
    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.
  • 7. Writing MEX files
    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 "gateway" between the MATLAB function call, and our C code.
  • 8. Writing MEX files
    In order to make a mex-function, you must include the "mex.h" library.
    #include "math.h"
    #include "mex.h"
  • 9. Writing MEX files
    There are four input parameters to the mexFunction:
    nlhs (Type = int)
    plhs (Type = array of pointers to mxArrays)
    nrhs (Type = int)
    prhs (Type = const array of pointers to mxArrays)
  • 10. Writing MEX files
    nlhs  represents the number of "left hand side" arguments.
    plhs  is the actual output arguments. An mxArray is MATLAB's structure for holding data and each element in plhs holds an mxArray of data.
    nrhs  holds the number of "right hand side" arguments.
    prhs  holds all of the pointers to the mxArrays of input data
  • 11. Writing MEX files
    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.
  • 12. Calling Built-In Functions from a MEX-File
    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.
  • 13. Calling Built-In Functions from a MEX-File
    mxArray *result;
    mxArray *arguments[2];
    arguments[0] = mxCreateDoubleMatrix(1, 20, mxREAL);
    arguments[1] = mxCreateDoubleMatrix(1, 10, mxREAL);
  • 14. Compiling a MEX file
    In the MATLAB command prompt:,
    Change the current directory to the location of the MEX source file.
    Type: mexfilename.c into the MATLAB command window.
    MATLAB may ask you to choose a compiler. Choose the compiler with MATLAB in its directory path.
    The new function will be called with the same name as our file.
  • 15. Using the MEX binary
    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. 
  • 16. Using the MEX binary
    On different systems, the MEX binary goes by different extensions: