Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C++
Article

MATLAB Generic DLL

Rate me:
Please Sign up or sign in to vote.
4.63/5 (26 votes)
18 Nov 20032 min read 441.7K   3K   44   103
Call a Dynamic Linked Library from MATLAB using MATLAB Interface to Generic DLLs.

Note: This article requires the MATLAB Interface to Generic Dlls. No link is available, but if you know of one please let us know.

Sample Image - MatlabGenericDll_1.jpg

Introduction

A shared library is a collection of functions that are available for use by one or more applications running on a system. On Windows operating systems, the library is compiled into a dynamic link library (.dll) file. At run-time, the library is loaded into memory and made accessible to all applications.

The MATLAB Interface to Generic DLLs enables you to interact with functions in dynamic link libraries directly from MATLAB.

Dynamic link libraries are easily accessed by MATLAB through a command line interface. This interface gives you the ability to load an external library into MATLAB memory space and then access any of the functions exported from library. Although data types differ between the two language environments, in most cases, you can pass MATLAB types to the C functions without having to do the work of conversion. MATLAB does this for you.

This interface also supports libraries containing functions programmed in languages other than C, provided that the functions have a C interface.

To give MATLAB access to external functions in a shared library, you must first load the library into memory. Once loaded, you can request information about any of the functions in the library and call them directly from MATLAB. When the library is no longer needed, you will need to unload it from memory to conserve memory usage.

Loading the Library

To load a shared library into MATLAB, use the loadlibrary function. The syntax for loadlibrary is:

loadlibrary('your_dll', 'header_file')

where your_dll is the filename for the .dll library file, and header_file is the filename for the header file that contains the function prototypes.

Here is an example:

loadlibrary('MatlabGenericDll', 'GenericDll.h')

Unloading the Library

To unload the library and free up the memory that it occupied, use the unloadlibrary function. For example:

unloadlibrary MatlabGenericDll

Getting Information About the Library

You can use either of these two functions to get information on the functions available in a library that you have loaded:

libmethods('libname')
libmethodsview('libname')

The main difference is that libmethods displays the information in the MATLAB Command Window (and you can assign its output to a variable), and libmethodsview displays the information as a graphical display in a new window (figure 2). To see what functions are available in the MatlabGenericDll library, use libmethods, specifying the library filename as the only argument.

libmethods MatlabGenericDll

LibMethodsView Window for displaying exported functions in a Dll

Invoking Library Functions

Once a library has been loaded into MATLAB, use the calllib function to call any of the functions from that library. Specify the library name, function name, and any arguments that get passed to the function:

calllib('libname', 'funcname', arg1, ..., argN)

Here is an example (demo project) to call Format dialog from MATLAB (See figure 1):

loadlibrary('MatlabGenericDll','GenericDll.h')
calllib('MatlabGenericDll', 'FormatDrive')
unloadlibrary MatlabGenericDll

For more information and instruction to setup this interface, refer to mathworks site or click here.

Enjoy!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
CEO Solaris Electronics LLC
United Arab Emirates United Arab Emirates
I was born in Shiraz, a very beautiful famous city in Iran. I started programming when I was 12 years old with GWBASIC. Since now, I worked with various programming languages from Basic, Foxpro, C/C++, Visual Basic, Pascal to MATLAB and now Visual C++.
I graduated from Iran University of Science & Technology in Communication Eng., and now work as a system programmer for a telecommunication industry.
I wrote several programs and drivers for Synthesizers, Power Amplifiers, GPIB, GPS devices, Radio cards, Data Acquisition cards and so many related devices.
I'm author of several books like Learning C (primary and advanced), Learning Visual Basic, API application for VB, Teach Yourself Object Oriented Programming (OOP) and etc.
I'm winner of January, May, August 2003 and April 2005 best article of month competition, my articles are:


You can see list of my articles, by clicking here


Comments and Discussions

 
AnswerRe: Calling MFC Dll in Matlab Pin
Abbas_Riazi18-Apr-06 4:42
professionalAbbas_Riazi18-Apr-06 4:42 
Generalpreprocessor error Pin
sunguy27-Jul-05 1:32
sunguy27-Jul-05 1:32 
GeneralSomething you should know in MATLAB R14 Pin
fusung12-Jul-05 1:07
fusung12-Jul-05 1:07 
QuestionCan generic dlls be called by ML 12 Pin
Gani B. Ganapathi18-Nov-04 11:46
sussGani B. Ganapathi18-Nov-04 11:46 
AnswerRe: Can generic dlls be called by ML 12 Pin
Gani B. Ganapathi18-Nov-04 12:03
sussGani B. Ganapathi18-Nov-04 12:03 
AnswerRe: Can generic dlls be called by ML 12 Pin
Abbas_Riazi18-Nov-04 19:36
professionalAbbas_Riazi18-Nov-04 19:36 
Generalinconsistent of loadlibrary with mcc statement Pin
mehdi_9714-Nov-04 1:59
mehdi_9714-Nov-04 1:59 
Generalmatlab version Pin
empedokles3-Nov-04 3:55
empedokles3-Nov-04 3:55 
GeneralRe: matlab version Pin
Abbas_Riazi3-Nov-04 6:21
professionalAbbas_Riazi3-Nov-04 6:21 
QuestionHow can I include Visual C++ header files when loading a library? Pin
x.miriam.x26-Oct-04 8:34
x.miriam.x26-Oct-04 8:34 
AnswerRe: How can I include Visual C++ header files when loading a library? Pin
Abbas_Riazi27-Oct-04 1:03
professionalAbbas_Riazi27-Oct-04 1:03 
AnswerRe: How can I include Visual C++ header files when loading a library? Pin
h5j1n96218-May-05 15:10
h5j1n96218-May-05 15:10 
GeneralGetting started with MATLAB Generic DLL Pin
Cincy26-Sep-04 8:50
Cincy26-Sep-04 8:50 
GeneralRe: Getting started with MATLAB Generic DLL Pin
Abbas_Riazi26-Sep-04 22:51
professionalAbbas_Riazi26-Sep-04 22:51 
GeneralRe: Getting started with MATLAB Generic DLL Pin
Cincy28-Sep-04 14:04
Cincy28-Sep-04 14:04 
GeneralPDF to TXT Pin
Anonymous30-Aug-04 21:40
Anonymous30-Aug-04 21:40 
GeneralRe: PDF to TXT Pin
Abbas_Riazi26-Sep-04 23:05
professionalAbbas_Riazi26-Sep-04 23:05 
GeneralPassing function arguments to a library function Pin
Fabien Viale4-Aug-04 21:59
sussFabien Viale4-Aug-04 21:59 
GeneralThe problem when I use this method. Pin
yuanyuansong19-Jul-04 9:41
yuanyuansong19-Jul-04 9:41 
GeneralRe: The problem when I use this method. Pin
Abbas_Riazi20-Jul-04 0:03
professionalAbbas_Riazi20-Jul-04 0:03 
GeneralRe: The problem when I use this method. Pin
Abbas_Riazi20-Jul-04 4:05
professionalAbbas_Riazi20-Jul-04 4:05 
GeneralRe: The problem when I use this method. Pin
yuanyuansong20-Jul-04 8:39
yuanyuansong20-Jul-04 8:39 
GeneralRe: The problem when I use this method. Pin
Abbas_Riazi20-Jul-04 23:37
professionalAbbas_Riazi20-Jul-04 23:37 
GeneralRe: The problem when I use this method. Pin
Abbas_Riazi21-Jul-04 4:17
professionalAbbas_Riazi21-Jul-04 4:17 
GeneralRe: The problem when I use this method. Pin
yuanyuansong21-Jul-04 4:35
yuanyuansong21-Jul-04 4:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.