External Interfaces Reference Previous page   Next Page
loadlibrary

Load external library into MATLAB

Syntax

Description

loadlibrary('shrlib', 'hfile') loads the functions defined in header file hfile and found in shared library shrlib into MATLAB. On Windows systems, shrlib refers to the name of a dynamic link library (.dll) file. On UNIX systems, it refers to the name of a shared object (.so) file.

loadlibrary('shrlib', @protofile) uses the prototype M-file protofile in place of a header file in loading the library shrlib. The string @protofile specifies a function handle to the prototype M-file. (See the description of "Prototype M-Files" below).

If you do not include a file extension with the shrlib argument, loadlibrary uses .dll or .so, depending on the platform you are using. If you do not include a file extension with the second argument, and this argument is not a function handle, loadlibrary uses .h for the extension.

loadlibrary('shrlib', ..., 'options') loads the library shrlib with one or more of the following options.

Option
Description
addheader hfileN

Loads the functions defined in the additional header file, hfileN. Specify the string hfileN as a filename without a file extension. MATLAB does not verify the existence of the header files and ignores any that are not needed.

You can specify as many additional header files as you need using the syntax

  • loadlibrary shrlib hfile ...
       addheader hfile1 ...
       addheader hfile2 ...      % and so on
    
alias name
Associates the specified alias name with the library. All subsequent calls to MATLAB functions that reference this library must use this alias until the library is unloaded.
includepath path
Specifies an additional path in which to look for included header files.
mfilename mfile
Generates a prototype M-file mfile in the current directory. You can use this file in place of a header file when loading the library. (See the description of "Prototype M-Files" below).

Only the alias option is available when loading using a prototype M-file.

If you have more than one library file of the same name, load the first using the library filename, and load the additional libraries using the alias option.

loadlibrary shrlib hfile options is the command format for this function.

Remarks

Prototype M-Files

When you use the mfilename option with loadlibrary, MATLAB generates an M-file called a prototype file. This file can then be used on subsequent calls to loadlibrary in place of a header file.

Like a header file, the prototype file supplies MATLAB with function prototype information for the library. You can make changes to the prototypes by editing this file and reloading the library.

Here are some reasons for using a prototype file, along with the changes you would need to make to the file:

Examples

Example 1

Use loadlibrary to load the MATLAB sample shared library, shrlibsample:

Example 2

Load sample library shrlibsample, giving it an alias name of lib. Once you have set an alias, you need to use this name in all further interactions with the library for this session:

Example 3

Load the library, specifying an additional path in which to search for included header files:

Example 4

Load the libmx library and generate a prototype M-file containing the prototypes defined in header file matrix.h:

Edit the generated file mxproto.m and locate the function 'mxGetNumberOfDimensions'. Give it an alias of 'mxGetDims' by adding this text to the line before fcnNum is incremented:

Here is the new function prototype. The change is shown in bold:

Unload the library and then reload it using the prototype M-file.

Now call mxGetNumberOfDimensions using the alias function name:

See Also

libisloaded, unloadlibrary, libfunctions, libfunctionsview, libpointer, libstruct, calllib


Previous page  libstruct unloadlibrary Next page

© 1994-2005 The MathWorks, Inc.