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

PPTX
Programming Environment in Matlab
PPTX
Procedures And Functions in Matlab
PPTX
C,C++ In Matlab
PPT
Database structure Structures Link list and trees and Recurison complete
PDF
EE6711 Power System Simulation Lab manual
PDF
PPT
linked list (c#)
PPT
Linked list
Programming Environment in Matlab
Procedures And Functions in Matlab
C,C++ In Matlab
Database structure Structures Link list and trees and Recurison complete
EE6711 Power System Simulation Lab manual
linked list (c#)
Linked list

What's hot (18)

PPT
Singly link list
PPTX
linked list
PPT
10 Linked Lists Sacks and Queues
PPTX
Ppt on Linked list,stack,queue
PPTX
single linked list
PPT
Stacks & Queues By Ms. Niti Arora
PPTX
Doubly & Circular Linked Lists
PPTX
Doubly linked list
PDF
Linked List Static and Dynamic Memory Allocation
PDF
Doubly Link List
PPT
linked list
PPT
Link List
PPT
358 33 powerpoint-slides_8-linked-lists_chapter-8
PPT
Operations on linked list
DOCX
Bitstuffing
PPT
Circular linked list
PPTX
Circular link list.ppt
Singly link list
linked list
10 Linked Lists Sacks and Queues
Ppt on Linked list,stack,queue
single linked list
Stacks & Queues By Ms. Niti Arora
Doubly & Circular Linked Lists
Doubly linked list
Linked List Static and Dynamic Memory Allocation
Doubly Link List
linked list
Link List
358 33 powerpoint-slides_8-linked-lists_chapter-8
Operations on linked list
Bitstuffing
Circular linked list
Circular link list.ppt
Ad

Viewers also liked (20)

PPTX
Matlab Image Restoration Techniques
PDF
Image Restoration (Digital Image Processing)
TXT
DOCX
basic of Image processing using matlab
PDF
Image processing
PDF
Basics of Image Processing using MATLAB
PDF
Introduction to matlab_application_to_electrical_engineering
PPTX
Simulink - Introduction with Practical Example
PDF
Modelling and Simulation of DC-Motor Electric Drive Control System with Varia...
PPT
Image degradation and noise by Md.Naseem Ashraf
PPTX
IEEE electrical projects using Matlab/Simulink software
PPTX
Matlab dc motor modeling
PPTX
How to Design of Power Management of Hybrid Circuit(Li-Ion Battery and Li-Ion...
PPTX
Speed control of dc motor using matlab
PDF
Digital Image Processing - Image Restoration
PPTX
Matlab for Electrical Engineers
PPTX
MINI ELECTRICAL PROJECT
PDF
Noise Models
PDF
filters for noise in image processing
PDF
Micro-controller based Automatic Power Factor Correction System Report
Matlab Image Restoration Techniques
Image Restoration (Digital Image Processing)
basic of Image processing using matlab
Image processing
Basics of Image Processing using MATLAB
Introduction to matlab_application_to_electrical_engineering
Simulink - Introduction with Practical Example
Modelling and Simulation of DC-Motor Electric Drive Control System with Varia...
Image degradation and noise by Md.Naseem Ashraf
IEEE electrical projects using Matlab/Simulink software
Matlab dc motor modeling
How to Design of Power Management of Hybrid Circuit(Li-Ion Battery and Li-Ion...
Speed control of dc motor using matlab
Digital Image Processing - Image Restoration
Matlab for Electrical Engineers
MINI ELECTRICAL PROJECT
Noise Models
filters for noise in image processing
Micro-controller based Automatic Power Factor Correction System Report
Ad

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

PPTX
intro2matlab-basic knowledge about Matlab.pptx
PDF
Introduction to matlab
PDF
Introduction to matlab
PDF
Matlab guide
PDF
Introduction to MATLAB
PPT
Introduction to matlab
TXT
Mex help hay vai
PDF
An ntutorial[1]
DOCX
DOC
Matlab summary
PPT
Brief Introduction to Matlab
PDF
lecture #1 & 2....education....matlab...helpful
PDF
interfacing matlab with embedded systems
PPSX
Matlab basic and image
PPTX
From zero to MATLAB hero: Mastering the basics and beyond
PPTX
Matlab Introduction
DOCX
KEVIN MERCHANT DOCUMENT
DOCX
Kevin merchantss
PDF
MATLAB INTRODUCTION
intro2matlab-basic knowledge about Matlab.pptx
Introduction to matlab
Introduction to matlab
Matlab guide
Introduction to MATLAB
Introduction to matlab
Mex help hay vai
An ntutorial[1]
Matlab summary
Brief Introduction to Matlab
lecture #1 & 2....education....matlab...helpful
interfacing matlab with embedded systems
Matlab basic and image
From zero to MATLAB hero: Mastering the basics and beyond
Matlab Introduction
KEVIN MERCHANT DOCUMENT
Kevin merchantss
MATLAB INTRODUCTION

More from matlab Content (20)

PPTX
Matlab: Control Statements
PPTX
Matlab: Discrete Linear Systems
PPTX
Matlab Distributions
PPTX
Matlab: Graph Plots
PPTX
Matlab: Gui
PPTX
Matlab: Linear Methods, Quantiles
PPTX
Matlab Data And Statistics
PPTX
Matlab Feature Extraction Using Segmentation And Edge Detection
PPTX
Matlab Image Enhancement Techniques
PPTX
Matlab Importing Data
PPTX
Matlab Organizing Data
PPTX
Matlab Text Files
PPTX
Matlab Visualizing Data
PPTX
Matlab Working With Images
PPTX
Matlab: Non Linear Methods
PPTX
Matlab: Procedures And Functions
PPTX
Matlab: Programming Environment
PPTX
Matlab: Regression
PPTX
Matlab: Saving And Publishing
PPTX
Matlab: Spectral Analysis, Fourier Analysis, Filterbank Analysis
Matlab: Control Statements
Matlab: Discrete Linear Systems
Matlab Distributions
Matlab: Graph Plots
Matlab: Gui
Matlab: Linear Methods, Quantiles
Matlab Data And Statistics
Matlab Feature Extraction Using Segmentation And Edge Detection
Matlab Image Enhancement Techniques
Matlab Importing Data
Matlab Organizing Data
Matlab Text Files
Matlab Visualizing Data
Matlab Working With Images
Matlab: Non Linear Methods
Matlab: Procedures And Functions
Matlab: Programming Environment
Matlab: Regression
Matlab: Saving And Publishing
Matlab: Spectral Analysis, Fourier Analysis, Filterbank Analysis

Recently uploaded (20)

PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Approach and Philosophy of On baking technology
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
Machine Learning_overview_presentation.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Electronic commerce courselecture one. Pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
A Presentation on Artificial Intelligence
PDF
cuic standard and advanced reporting.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
Dropbox Q2 2025 Financial Results & Investor Presentation
MIND Revenue Release Quarter 2 2025 Press Release
“AI and Expert System Decision Support & Business Intelligence Systems”
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Approach and Philosophy of On baking technology
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Network Security Unit 5.pdf for BCA BBA.
NewMind AI Weekly Chronicles - August'25-Week II
Machine Learning_overview_presentation.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Encapsulation_ Review paper, used for researhc scholars
Electronic commerce courselecture one. Pdf
Empathic Computing: Creating Shared Understanding
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
A Presentation on Artificial Intelligence
cuic standard and advanced reporting.pdf
Big Data Technologies - Introduction.pptx
Unlocking AI with Model Context Protocol (MCP)

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