• Save
C,C++ In Matlab
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

C,C++ In Matlab

on

  • 10,554 views

C,C++ In Matlab

C,C++ In Matlab

Statistics

Views

Total Views
10,554
Views on SlideShare
10,358
Embed Views
196

Actions

Likes
4
Downloads
0
Comments
0

3 Embeds 196

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

Accessibility

Categories

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.

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

C,C++ In Matlab Presentation Transcript

  • 1. Integrating C, C++ with MATLAB
  • 2. Why integrate C/C++ with MATLAB?
    Speed:
    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
    Speed
  • 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
    Example:
    mxArray *result;
    mxArray *arguments[2];
    arguments[0] = mxCreateDoubleMatrix(1, 20, mxREAL);
    arguments[1] = mxCreateDoubleMatrix(1, 10, mxREAL);
    mexCallMATLAB(1,&result,2,arguments,"conv");
  • 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:
    Windows=.dll
    MacOSX=.mexmac
    Solaris=.mexsol
    Linux=.mexlx