SlideShare a Scribd company logo
Integrating C, C++ with MATLAB
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.
Why integrate C/C++ with MATLAB?Data Structure ManagementSpeed
Integrating C++ with MATLABFor integration of C/C++ with MATLAB, we make use of MEX filesMEX files are pre-compiled files that are called from MATLABMEX files can be compiled from Matlab.m filesMEX files can also be compiled from external C/C++ code.
MEX files
Components of a MEX fileEvery 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 filesIt 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 filesIn order to make a mex-function, you must include the "mex.h" library.#include "math.h" #include   "mex.h"
Writing MEX filesThere 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 filesnlhs  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 filesmxArrayis 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-FileMATLAB 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-FileExample:mxArray *result;mxArray *arguments[2];arguments[0] = mxCreateDoubleMatrix(1, 20, mxREAL);arguments[1] = mxCreateDoubleMatrix(1, 10, mxREAL);mexCallMATLAB(1,&result,2,arguments,"conv");
Compiling a MEX fileIn 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 binaryAfter 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 binaryOn different systems, the MEX binary goes by different extensions:Windows=.dllMacOSX=.mexmacSolaris=.mexsolLinux=.mexlx

More Related Content

What's hot (20)

PDF
Introduction to MATLAB
Sarah Hussein
 
PPT
Matlab introduction
Satish Gummadi
 
PPTX
All About MATLAB
Multisoft Virtual Academy
 
DOCX
B61301007 matlab documentation
Manchireddy Reddy
 
PPTX
Matlab 1(operations on_matrix)
harman kaur
 
PPTX
Procedures And Functions in Matlab
DataminingTools Inc
 
PDF
Advanced MATLAB Tutorial for Engineers & Scientists
Ray Phan
 
PDF
Matlab-Data types and operators
Luckshay Batra
 
PPT
Introduction to matlab
Mohammad Tawfik
 
PPTX
Importance of matlab
krajeshk1980
 
PDF
Matlab solved problems
Make Mannan
 
PDF
A complete introduction on matlab and matlab's projects
Mukesh Kumar
 
PDF
Introduction to matlab
BinodKumarSahu5
 
PDF
EE6711 Power System Simulation Lab manual
Velalar College of Engineering and Technology
 
PDF
Introduction to MATLAB
Dun Automation Academy
 
PDF
Introduction to matlab
Sourabh Bhattacharya
 
PDF
Introduction to MATLAB 1
Mohamed Gafar
 
PPTX
Matlab matrices and arrays
Ameen San
 
Introduction to MATLAB
Sarah Hussein
 
Matlab introduction
Satish Gummadi
 
All About MATLAB
Multisoft Virtual Academy
 
B61301007 matlab documentation
Manchireddy Reddy
 
Matlab 1(operations on_matrix)
harman kaur
 
Procedures And Functions in Matlab
DataminingTools Inc
 
Advanced MATLAB Tutorial for Engineers & Scientists
Ray Phan
 
Matlab-Data types and operators
Luckshay Batra
 
Introduction to matlab
Mohammad Tawfik
 
Importance of matlab
krajeshk1980
 
Matlab solved problems
Make Mannan
 
A complete introduction on matlab and matlab's projects
Mukesh Kumar
 
Introduction to matlab
BinodKumarSahu5
 
EE6711 Power System Simulation Lab manual
Velalar College of Engineering and Technology
 
Introduction to MATLAB
Dun Automation Academy
 
Introduction to matlab
Sourabh Bhattacharya
 
Introduction to MATLAB 1
Mohamed Gafar
 
Matlab matrices and arrays
Ameen San
 

Viewers also liked (20)

PPT
Paramount Search Partners
jjmcdermott
 
PPTX
MS SQL SERVER: Programming sql server data mining
DataminingTools Inc
 
PPTX
DataKraft - Powerful No-Coding Platform for Business Applications
Tibbs Pereira
 
PPTX
Drc 2010 D.J.Pawlik
slrommel
 
PPT
Épica Latina Latín II
lara
 
PPTX
Clickthrough
dpapageorge
 
PPTX
Txomin Hartz Txikia
irantzugoitia86
 
PPT
Association Rules
DataminingTools Inc
 
PPTX
Data Applied:Forecast
DataminingTools Inc
 
PPTX
MS Sql Server: Reporting introduction
DataminingTools Inc
 
PPTX
LISP:Object System Lisp
DataminingTools Inc
 
PDF
Apresentação Red Advisers
mezkita
 
PPT
2008 IEDM presentation
slrommel
 
PPTX
Data Applied: Similarity
DataminingTools Inc
 
PPTX
LISP: Macros in lisp
DataminingTools Inc
 
PPTX
Simulation
DataminingTools Inc
 
PPTX
XL-MINER:Prediction
DataminingTools Inc
 
PPT
Festivals Refuerzo
guest9536ef5
 
PDF
Bind How To
cntlinux
 
Paramount Search Partners
jjmcdermott
 
MS SQL SERVER: Programming sql server data mining
DataminingTools Inc
 
DataKraft - Powerful No-Coding Platform for Business Applications
Tibbs Pereira
 
Drc 2010 D.J.Pawlik
slrommel
 
Épica Latina Latín II
lara
 
Clickthrough
dpapageorge
 
Txomin Hartz Txikia
irantzugoitia86
 
Association Rules
DataminingTools Inc
 
Data Applied:Forecast
DataminingTools Inc
 
MS Sql Server: Reporting introduction
DataminingTools Inc
 
LISP:Object System Lisp
DataminingTools Inc
 
Apresentação Red Advisers
mezkita
 
2008 IEDM presentation
slrommel
 
Data Applied: Similarity
DataminingTools Inc
 
LISP: Macros in lisp
DataminingTools Inc
 
XL-MINER:Prediction
DataminingTools Inc
 
Festivals Refuerzo
guest9536ef5
 
Bind How To
cntlinux
 
Ad

Similar to C,C++ In Matlab (20)

TXT
Mex help hay vai
Dang Hop
 
PPTX
intro2matlab-basic knowledge about Matlab.pptx
uf5221985
 
PPTX
Matlab: Procedures And Functions
matlab Content
 
TXT
Mex help
Yohannes Masterous
 
PPTX
Matlab & C/C++ Interacting
Hamid Jamali
 
PPT
Matlab Tutorial.ppt
RaviMuthamala1
 
PPT
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
reddyprasad reddyvari
 
PPT
Matlab Introduction
Daniel Moore
 
PDF
Matlab for beginners, Introduction, signal processing
Dr. Manjunatha. P
 
PDF
++Matlab 14 sesiones
Rosemberth Rodriguez
 
PPTX
Matlab Introduction
ideas2ignite
 
PDF
Presentation21.pdf
HassanHamada7
 
PPT
matlab tutorial with separate function description and handson learning
vishalkumarpandey12
 
PPT
MATLAB_CIS601-03.ppt
aboma2hawi
 
PDF
Introduction to MATrices LABoratory (MATLAB) as Part of Digital Signal Proces...
Ahmed Gad
 
PPTX
Matlab - Introduction and Basics
Techsparks
 
PDF
Tutorialmatlab kurniawan.s
Kurniawan susanto
 
PDF
Tutorial matlab
Kavin Patel
 
PPT
INTRODUCTION TO MATLAB for PG students.ppt
Karthik537368
 
PDF
Section-6-User-Defined-Functions.pdf
johnkyllelumacang699
 
Mex help hay vai
Dang Hop
 
intro2matlab-basic knowledge about Matlab.pptx
uf5221985
 
Matlab: Procedures And Functions
matlab Content
 
Matlab & C/C++ Interacting
Hamid Jamali
 
Matlab Tutorial.ppt
RaviMuthamala1
 
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
reddyprasad reddyvari
 
Matlab Introduction
Daniel Moore
 
Matlab for beginners, Introduction, signal processing
Dr. Manjunatha. P
 
++Matlab 14 sesiones
Rosemberth Rodriguez
 
Matlab Introduction
ideas2ignite
 
Presentation21.pdf
HassanHamada7
 
matlab tutorial with separate function description and handson learning
vishalkumarpandey12
 
MATLAB_CIS601-03.ppt
aboma2hawi
 
Introduction to MATrices LABoratory (MATLAB) as Part of Digital Signal Proces...
Ahmed Gad
 
Matlab - Introduction and Basics
Techsparks
 
Tutorialmatlab kurniawan.s
Kurniawan susanto
 
Tutorial matlab
Kavin Patel
 
INTRODUCTION TO MATLAB for PG students.ppt
Karthik537368
 
Section-6-User-Defined-Functions.pdf
johnkyllelumacang699
 
Ad

More from DataminingTools Inc (20)

PPTX
Terminology Machine Learning
DataminingTools Inc
 
PPTX
Techniques Machine Learning
DataminingTools Inc
 
PPTX
Machine learning Introduction
DataminingTools Inc
 
PPTX
Areas of machine leanring
DataminingTools Inc
 
PPTX
AI: Planning and AI
DataminingTools Inc
 
PPTX
AI: Logic in AI 2
DataminingTools Inc
 
PPTX
AI: Logic in AI
DataminingTools Inc
 
PPTX
AI: Learning in AI 2
DataminingTools Inc
 
PPTX
AI: Learning in AI
DataminingTools Inc
 
PPTX
AI: Introduction to artificial intelligence
DataminingTools Inc
 
PPTX
AI: Belief Networks
DataminingTools Inc
 
PPTX
AI: AI & Searching
DataminingTools Inc
 
PPTX
AI: AI & Problem Solving
DataminingTools Inc
 
PPTX
Data Mining: Text and web mining
DataminingTools Inc
 
PPTX
Data Mining: Outlier analysis
DataminingTools Inc
 
PPTX
Data Mining: Mining stream time series and sequence data
DataminingTools Inc
 
PPTX
Data Mining: Mining ,associations, and correlations
DataminingTools Inc
 
PPTX
Data Mining: Graph mining and social network analysis
DataminingTools Inc
 
PPTX
Data warehouse and olap technology
DataminingTools Inc
 
PPTX
Data Mining: Data processing
DataminingTools Inc
 
Terminology Machine Learning
DataminingTools Inc
 
Techniques Machine Learning
DataminingTools Inc
 
Machine learning Introduction
DataminingTools Inc
 
Areas of machine leanring
DataminingTools Inc
 
AI: Planning and AI
DataminingTools Inc
 
AI: Logic in AI 2
DataminingTools Inc
 
AI: Logic in AI
DataminingTools Inc
 
AI: Learning in AI 2
DataminingTools Inc
 
AI: Learning in AI
DataminingTools Inc
 
AI: Introduction to artificial intelligence
DataminingTools Inc
 
AI: Belief Networks
DataminingTools Inc
 
AI: AI & Searching
DataminingTools Inc
 
AI: AI & Problem Solving
DataminingTools Inc
 
Data Mining: Text and web mining
DataminingTools Inc
 
Data Mining: Outlier analysis
DataminingTools Inc
 
Data Mining: Mining stream time series and sequence data
DataminingTools Inc
 
Data Mining: Mining ,associations, and correlations
DataminingTools Inc
 
Data Mining: Graph mining and social network analysis
DataminingTools Inc
 
Data warehouse and olap technology
DataminingTools Inc
 
Data Mining: Data processing
DataminingTools Inc
 

Recently uploaded (20)

PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PPTX
Darren Mills The Migration Modernization Balancing Act: Navigating Risks and...
AWS Chicago
 
PDF
Ampere Offers Energy-Efficient Future For AI And Cloud
ShapeBlue
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
July Patch Tuesday
Ivanti
 
PDF
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
PPTX
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
PPTX
Building and Operating a Private Cloud with CloudStack and LINBIT CloudStack ...
ShapeBlue
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
Empowering Cloud Providers with Apache CloudStack and Stackbill
ShapeBlue
 
PDF
Rethinking Security Operations - SOC Evolution Journey.pdf
Haris Chughtai
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
Darren Mills The Migration Modernization Balancing Act: Navigating Risks and...
AWS Chicago
 
Ampere Offers Energy-Efficient Future For AI And Cloud
ShapeBlue
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
July Patch Tuesday
Ivanti
 
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
Building and Operating a Private Cloud with CloudStack and LINBIT CloudStack ...
ShapeBlue
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
Empowering Cloud Providers with Apache CloudStack and Stackbill
ShapeBlue
 
Rethinking Security Operations - SOC Evolution Journey.pdf
Haris Chughtai
 

C,C++ In Matlab

  • 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 ManagementSpeed
  • 4. Integrating C++ with MATLABFor integration of C/C++ with MATLAB, we make use of MEX filesMEX files are pre-compiled files that are called from MATLABMEX files can be compiled from Matlab.m filesMEX files can also be compiled from external C/C++ code.
  • 6. Components of a MEX fileEvery 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 filesIt 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 filesIn order to make a mex-function, you must include the "mex.h" library.#include "math.h" #include "mex.h"
  • 9. Writing MEX filesThere 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 filesnlhs  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 filesmxArrayis 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-FileMATLAB 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-FileExample: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 fileIn 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 binaryAfter 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 binaryOn different systems, the MEX binary goes by different extensions:Windows=.dllMacOSX=.mexmacSolaris=.mexsolLinux=.mexlx