This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Connexions module: m12348 1 Writing C Functions in MATLAB (MEX-Files) * Jason Laska This work is produced by The Connexions Project and licensed under the Creative Commons Attribution License † Abstract This is a basic guide to writing mex les for MATLAB in C. This guide gives a simple background of some the MATLAB mex features so that learning to write mex les can be quick and easy. 1 Introduction The MATLAB M-File is very good for putting together functions or scripts that run many of MATLAB's fast Built-In functions. One nice thing about these les is that they are never compiled and will run on any system that is already running MATLAB. MATLAB achieves this by interpreting each line of the M-File every time it is run. This method of running the code can make processing time very slow for large and complicated functions, especially those with many loops because every line within the loop will be interpreted as a new line, each time through the loop. Good MATLAB code avoids these things by using as many Built- In features and array operations as possible (because these are fast and e cient). Sometimes this is not enough... MATLAB has the capability of running functions written in C. The les which hold the source for these functions are called MEX-Files. The mexFunctions are not intended to be a substitue for MATLAB's Built- In operations however if you need to code many loops and other things that MATLAB is not very good at, this is a good option. This feature also allows system-speci c APIs to be called to extend MATLAB's abilities (see the Serial Port Tutorial for an example of this). This document is arranged in the following manner: • The MEX-Function: Interface to MATLAB • Getting and Creating Data • Calling Built-In Functions from a MEX-File • Compiling • Useful Functions not Mentioned Here These are some of the basic topics that will allow you to create a MEX- le in a short time. There are many other features and abilities that MATLAB has which can be explored in the MATLAB documentation. * Version 1.2: Aug 1, 2004 5:46 pm GMT-5 † http://creativecommons.org/licenses/by/1.0 http://cnx.org/content/m12348/1.2/ Connexions module: m12348 2 2 The MEX-Function: Interface to MATLAB When writing programs in C, it is always assumed that the program will start execution from the main(). MEX -Files are similar in that they always start execution from a special function called the mexFunction....
View Full Document