User`s manual
Examples of C MEX-Files
2-7
Examples of C MEX-Files
The following sections include information and examples describing how to 
pass and manipulate the different data types when working with MEX-files. 
These topics include
•“A First Example — Passing a Scalar”
•“Passing Strings”
•“Passing Two or More Inputs or Outputs”
•“Passing Structures and Cell Arrays”
•“Handling Complex Data”
•“Handling 8-,16-, and 32-Bit Data”
•“Manipulating Multidimensional Numerical Arrays”
•“Handling Sparse Arrays”
•“Calling Functions from C MEX-Files”
The MATLAB API provides a full set of routines that handle the various data 
types supported by MATLAB. For each data type there is a specific set of 
functions that you can use for data manipulation. The first example discusses 
the simple case of doubling a scalar. After that, the examples discuss how to 
pass in, manipulate, and pass back various data types, and how to handle 
multiple inputs and outputs. Finally, the sections discuss passing and 
manipulating various MATLAB data types.
Note You can find the most recent versions of the example programs at the 
anonymous FTP server
ftp://ftp.mathworks.com/pub/tech-support/docexamples/apiguide/R12/refbook
A First Example — Passing a Scalar
Let’s look at a simple example of C code and its MEX-file equivalent. Here is a 
C computational function that takes a scalar and doubles it.
#include <math.h>
void timestwo(double y[], double x[])










