Pass Arguments to Shared C Library Functions
C and MATLAB Equivalent Types
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.
All scalar values returned by MATLAB are of type
MATLAB Primitive Types
|C Type||Equivalent MATLAB Type|
cell array of character vectors
The following table shows how MATLAB maps C pointers (column 1) to the equivalent MATLAB function signature (column 2). Usually, 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 to use a
lib.pointer object instead.
MATLAB Extended Types
|C Pointer Type||Argument|
Example Function in
cell array of character vectors
How MATLAB Displays Function Signatures
Here are things to note about the input and output arguments shown in MATLAB function signatures.
Many arguments (like
double) are similar to their C counterparts. In these cases, 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, either pass a standard MATLAB type and let MATLAB convert it for you, or convert the data yourself using the MATLAB functions
libpointer. For more information, see Manually Convert Data Passed to Functions.
C functions often return data in input arguments passed by reference. MATLAB creates additional output arguments to return these values. Input arguments ending in
PtrPtrare also listed as outputs.
For an example of MATLAB function signatures, see Shared Library shrlibsample.
Guidelines for Passing Arguments
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. 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
NULLparameter to a library function that supports optional input arguments. This notation is valid only when the argument is declared as a
PtrPtras shown by
You can create a
NULL pointer to pass to library functions in
the following ways:
Pass an empty array
as the argument.
p = libpointer; % no arguments
p = libpointer('string') % string argument
p = libpointer('cstring') % pointer to a string argument
p = libstruct('structtype'); % structure type
To create an empty
libstruct object, call
libstruct with only the
argument. For example:
sci = libstruct('c_struct') get(sci)
p1: 0 p2: 0 p3: 0
MATLAB displays the initialized values.
Manually Convert Data Passed to Functions
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 might choose to convert your argument data manually. For example:
When passing the same data to a series of library functions, convert it once manually before calling the first function rather than having MATLAB convert it automatically on every call. This strategy reduces the number of unnecessary copy and conversion operations.
When passing large structures, save memory by creating MATLAB structures that match the shape of the C structures used in the function instead of using generic MATLAB structures. The
libstructfunction creates a MATLAB structure modeled from a C structure taken from the library.
When an argument to an external function uses more than one level of referencing (for example,
double **), pass a pointer created using the
libpointerfunction rather than relying on MATLAB to convert the type automatically.