Passing Arguments to Shared 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.For examples using these arguments, see Passing Primitive Types and Passing Strings.

MATLAB Primitive Types

C TypeEquivalent MATLAB Type

char, byte

int8

unsigned char, byte

uint8

short

int16

unsigned short

uint16

int

int32

long (32-bit)

int32

long (64-bit)

int64

unsigned int, unsigned long

uint32

float

single

double

double

char *

cstring (1xn char array)

*char[]

cell array of strings

The following table shows extended MATLAB types in the right column. These are instances of the MATLAB lib.pointer class rather than standard MATLAB types. For information on the lib.pointer class, see Working with Pointers. For an example using pointer arguments, see Passing a Reference.

MATLAB Extended Types

C TypeEquivalent MATLAB Type

integer pointer types (int *)

(u)int(size)Ptr

Null-terminated string passed by value

cstring

Null-terminated string passed by reference (from a libpointer only)

stringPtr

Array of pointers to strings (or one **char)

stringPtrPtr

Matrix of signed bytes

int8Ptr

float *

singlePtr

double *

doublePtr

mxArray *

MATLAB array

void *

voidPtr

void **

voidPtrPtr

type **

Same as typePtr with an added Ptr (e.g., double **
is doublePtrPtr)

Passing Arguments

Here are some important things to note about the input and output arguments shown in the Functions in library shrlibsample listing:

Guidelines for Passing Arguments

Examples of Passing Data to Shared Libraries

Sample Shared Library shrlibsample

MATLAB software includes a sample external library called shrlibsample. The library is in the directory matlabroot\extern\examples\shrlib.

To use the shrlibsample library, you first need to either add this directory to your MATLAB path with the command:

addpath([matlabroot '\extern\examples\shrlib'])

or make the directory your current working directory with the command:

cd([matlabroot '\extern\examples\shrlib'])

The following example loads the shrlibsample library and displays the MATLAB syntax for calling functions in the library:

loadlibrary shrlibsample shrlibsample.h
libfunctions shrlibsample -full

MATLAB displays:

Functions in library shrlibsample:

[double, doublePtr] addDoubleRef(double, doublePtr, double)
double addMixedTypes(int16, int32, double)
[double, c_structPtr] addStructByRef(c_structPtr)
double addStructFields(c_struct)
c_structPtrPtr allocateStruct(c_structPtrPtr)
voidPtr deallocateStruct(voidPtr)
doublePtr multDoubleArray(doublePtr, int32)
[lib.pointer, doublePtr] multDoubleRef(doublePtr)
int16Ptr multiplyShort(int16Ptr, int32)
cstring readEnum(Enum1)
[cstring, cstring] stringToUpper(cstring)

Passing Primitive Types

For primitive types, MATLAB automatically converts any argument to the type expected by the external function. For example, you can pass a double to a function that expects to receive a byte (8-bit integer) and MATLAB does the conversion for you.

The following C function takes arguments that are of types short, int, and double:

double addMixedTypes(short x, int y, double z) {
   return (x + y + z);
}

You can pass all of the arguments as type double from MATLAB. MATLAB determines what type of data is expected for each argument and performs the appropriate conversions. For example, type:

calllib('shrlibsample', 'addMixedTypes', 127, 33000, pi)

MATLAB displays:

ans =
  3.3130e+004

Passing a Reference

MATLAB automatically converts an argument passed by value into an argument passed by reference when the external function prototype defines the argument as a reference. For example, a MATLAB double argument passed to a function that expects double * is converted to a double reference by MATLAB.

addDoubleRef is a C function that takes an argument of type double *:

double addDoubleRef(double x, double *y, double z) {
    return (x + *y + z);
}

Call the function with three arguments of type double, and MATLAB handles the conversion:

calllib('shrlibsample', 'addDoubleRef', 1.78, 5.42, 13.3)

MATLAB displays:

ans =
   20.5000

Passing Strings

For arguments that require char *, you can pass a MATLAB string (a character array).

For example, the following C function takes a char * input argument:

char* stringToUpper(char *input) {
   char *p = input;

   if (p != NULL)
      while (*p!=0)
         *p++ = toupper(*p);
   return input;
}

libfunctions shows that you can use a MATLAB cstring for this input. Type:

libfunctions shrlibsample -full

Look for the following stringToUpper signature:

[cstring, cstring] stringToUpper(cstring)

Create a MATLAB character array, str, and pass it as the input argument:

str = 'This was a Mixed Case string';
calllib('shrlibsample', 'stringToUpper', str)

MATLAB displays:

ans =
   THIS WAS A MIXED CASE STRING

Although the input argument that MATLAB passes to stringToUpper resembles a reference to type char, it is not a true reference data type because it does not contain the address of the MATLAB character array, str. When the function executes, it returns the correct result, but does not modify the value in str. If you examine str, you find it is unchanged. Type:

str

MATLAB displays:

str = 
  This was a Mixed Case string

Passing Enumerated Types

For arguments defined as C enumerated types, you can pass either the enumeration string or its integer equivalent.

The readEnum function from the shrlibsample library returns the enumeration string that matches the argument passed in. Here is the Enum1 definition and the readEnum function in C:

enum Enum1 {en1 = 1, en2, en4 = 4} TEnum1;

char* readEnum(TEnum1 val) {
  switch (val) {
    case 1 :return "You chose en1";
    case 2: return "You chose en2";
    case 4: return "You chose en4";
    default : return "enum not defined";
  }
}

In MATLAB, you can express an enumerated type as either the enumeration string or its equivalent numeric value. In the previous example, the TEnum1 definition declares enumeration en4 equal to 4. Call readEnum first with a string:

calllib('shrlibsample', 'readEnum', 'en4')

MATLAB displays:

ans =
   You chose en4

Now call it with the equivalent numeric argument 4:

calllib('shrlibsample', 'readEnum', 4)

MATLAB displays:

ans =
   You chose en4

Passing References

Many functions in external libraries use arguments that are passed by reference. To enable you to interact with these functions, MATLAB passes what is called a pointer object to these arguments. This should not be confused with "passing by reference" in the typical sense of the term. See Working with Pointers for more information.

Passing a NULL Pointer

You can create a NULL pointer to pass to library functions in the following ways:

Manually Converting 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 may choose to convert your argument data manually. Circumstances under which you might find this advantageous are:

Using C++ Libraries

To load C++ libraries, you must define the function prototypes as extern "C" in the library header file. For example, the following function prototype from the file mex.h shows the syntax to use for each function:

#ifdef __cplusplus
extern "C" {
#endif
void mexFunction(
    int           nlhs,
    mxArray       *plhs[], 
    int           nrhs,
    const mxArray *prhs[]
);
#ifdef __cplusplus
}
#endif

Another approach to using C++ libraries is to generate a prototype M-file that contain aliases for the mangled C++ function names. Use the original (premangled) function names as the aliases for the C++ functions. Generate the M-file with the mfilename option of the loadlibrary function and then determine which functions in the library you want to make available by defining aliases for these functions.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS