• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
C,C++ In Matlab

C,C++ In Matlab



C,C++ In Matlab

C,C++ In Matlab



Total Views
Views on SlideShare
Embed Views



3 Embeds 196

http://www.dataminingtools.net 132
http://dataminingtools.net 63
http://www.slideshare.net 1



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    C,C++ In Matlab C,C++ In Matlab Presentation Transcript

    • Integrating C, C++ with MATLAB
    • 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.
    • Why integrate C/C++ with MATLAB?
      Data Structure Management
    • 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.
    • MEX files
    • 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.
    • 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.
    • Writing MEX files
      In order to make a mex-function, you must include the "mex.h" library.
      #include "math.h"
      #include "mex.h"
    • 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)
    • 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
    • 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.
    • 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.
    • 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);
    • 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.
    • 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. 
    • Using the MEX binary
      On different systems, the MEX binary goes by different extensions: