|On this page…|
The shared library interface supports all standard scalar C types. The following table shows these C types with their equivalent MATLAB® types. MATLAB uses the type from the right column for arguments having the C type shown in the left column.
MATLAB Primitive Types
|C Type||Equivalent MATLAB Type|
unsigned char, byte
|unsigned long (Windows)|
|unsigned long (Linux)|
1xn char array
cell array of strings
The following table shows how MATLAB maps C pointers (column 1) to the equivalent MATLAB function signature (column 2). In most cases, you can pass a variable from the Equivalent MATLAB Type column to functions with the corresponding Argument Data Type. See Pointer Arguments in C Functions for information about when you might choose to use a lib.pointer object instead.
MATLAB Extended Types
|C Pointer Type||Argument Data Type||Equivalent MATLAB Type|
integer pointer types (int *)
Matrix of signed bytes
Null-terminated string passed by value
1xn char array
Array of pointers to strings (or one **char)
cell array of strings
Same as typePtr with
an added Ptr (for example, double **
Here are some important things to note about the input and output arguments shown in the Functions in library shrlibsample listing:
Many arguments (like int32 and double) are similar to their C counterparts. In these cases, you need only to pass in the MATLAB types shown for these arguments.
Some C arguments (for example, **double, or predefined structures), are different from standard MATLAB types. In these cases, you can either pass a standard MATLAB type and let MATLAB convert it for you, or you convert the data yourself using the MATLAB functions libstruct and libpointer. For more information, see Manually Converting Data Passed to Functions.
C functions often return data in input arguments passed by reference. MATLAB creates additional output arguments to return these values. Note that in the listing in the previous section, all input arguments ending in Ptr or PtrPtr are also listed as outputs.
Nonscalar arguments must be declared as passed by reference in the library functions.
If the library function uses single subscript indexing to reference a two-dimensional matrix, keep in mind that C programs process matrices row by row while MATLAB processes matrices by column. To get C behavior from the function, transpose the input matrix before calling the function, and then transpose the function output.
Use an empty array, , to pass a NULL parameter to a library function that supports optional input arguments. This is valid only when the argument is declared as a Ptr or PtrPtr as shown by libfunctions or libfunctionsview.
You can create a NULL pointer to pass to library functions in the following ways:
Pass an empty array  as the argument.
Use the libpointer function:
p = libpointer; % no arguments
p = libpointer('string') % string argument
p = libpointer('stringPtr') % pointer to a string argument
Use the libstruct function:
p = libstruct('structtype'); % structure type
To create an empty libstruct object, call libstruct with only the structtype argument. For example:
sci = libstruct('c_struct') get(sci)
p1: 0 p2: 0 p3: 0
MATLAB displays the initialized values.
Under most conditions, MATLAB software automatically converts data passed to and from external library functions to the type expected by the external function. However, you may choose to convert your argument data manually. Circumstances under which you might find this advantageous are:
When you pass the same piece of data to a series of library functions, you can convert it once manually before the call to the first function rather than having MATLAB convert it automatically on every call. This reduces the number of unnecessary copy and conversion operations.
When you pass large structures, you can save memory by creating MATLAB structures that match the shape of the C structures used in the external function instead of using generic MATLAB structures. The libstruct function creates a MATLAB structure modeled from a C structure taken from the library. See Working with Structure Arguments for more information.
When an argument to an external function uses more than one level of referencing (e.g., double **), you must pass a pointer created using the libpointer function rather than relying on MATLAB to convert the type automatically.