Technical Solutions
Is it possible to call a dynamic link library from MATLAB?
Date Last Modified: Wednesday, October 21, 2009
| Solution ID: |
|
1-1ABRP |
| Product: |
|
MATLAB |
| Reported in Release: |
|
R13 |
| Fixed in Release: |
|
R2008a |
| Platform: |
|
Windows |
| Operating System: |
|
Windows Any |
Subject:
Is it possible to call a dynamic link library from MATLAB?
Problem Description:
All operating systems provide a collection of functions precompiled into a shared library file (a dynamic link library, or .dll, in Windows). These libraries, provided by the operating system and other software vendors, are designed for use by one or more applications running on the system. There is significant value in being able to access the shared library functions from inside the MATLAB environment.
Solution:
Starting from MATLAB 7.6 (R2008a), LOADLIBRARY interface allows loading 64- bit shared libraries on supported 64-bit platforms.
For previous product releases, read below for any possible workarounds: The LOADLIBRARY function can be used to access functions in shared libraries. On Windows, these files are called Dynamically Linked Libraries (DLLs), and have extension ".dll"; on other systems, they are called Shared Libraries and usually have extension ".so".
The LOADLIBRARY command is available for 32-bit Windows in MATLAB 6.5.1 (R13SP1) and later, and for 32-bit Linux in MATLAB 7.0.4 (R14SP2) and later. This functionality can be added to MATLAB 6.5 (R13) for Windows with the patch below.
For more information on accessing shared libraries using LOADLIBRARY, consult the Generic DLL section of the documentation:
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/f23224dfi7.html
For other platforms or earlier releases, you can use a MEX-file that calls the C or Fortran function LoadLibrary(). You can find more information about MEX-files here:
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/f23224.html
The LOADLIBRARY function allows you to load a shared library into the MATLAB memory space and then access any of its functions. Because the DLL may contain functions programmed in languages other than C, the DLL must provide a C interface so that you can load and use the functions from within MATLAB.
If you are using MATLAB 6.5 (R13) on 32-bit Windows, you should download GenericDll_1p1.exe (linked at the bottom of the page) needed to add this new functionality.
Note: The patch only applies to MATLAB 6.5 (R13). Later versions of MATLAB ship with this functionality and your installation will be damaged if you install the download.
To install:
1. Quit any current MATLAB sessions.
2. Download the genericDll.exe file (336 K). The download consists of a self-extracting executable that installs the necessary files into your MATLAB directory.
3. Save the downloadable to your local hard disk.
4. Double-click the executable to start the installation on your local PC. The WinZip Self-Extractor dialog opens, indicating the version of the update.
5. Read the comments in the WinZip Self-Extractor dialog, and then click OK. A dialog opens with the default location for MATLAB root as C:\MATLAB6p5. If MATLAB is installed in a different directory on your machine, specify that location instead.
6. Click the Unzip button to install the interface files in the appropriate directories. After the files are extracted and installed, a new dialog box opens and displays a message indicating that files have been successfully unzipped. The installation is complete. Click OK to close.
7. Exit the installation program by clicking Close in the WinZip Self-Extractor dialog box. Your interface to shared libraries is now ready for use.
8. In order for MATLAB to recognize the newly installed files, refresh your Toolbox Cache with the following command:
rehash toolboxcache
|
Related Documents/Files: