Click here to Skip to main content
15,867,832 members
Articles / Programming Languages / C

Connecting MATLAB to C-language DLLs

Rate me:
Please Sign up or sign in to vote.
3.45/5 (4 votes)
27 Nov 2006CPOL3 min read 95.1K   1.9K   20   8
The MATLAB environment does not allow for easy access to common Windows functions; this article explains how to access these easily from within MATLAB, by calling a specially developed C DLL library

Introduction

MATLAB is an advanced programming environment for developing specialist mathematical, algorithmic, or scientific applications. This environment has many benefits for this type of development, but accessing common Windows development functionality isn't so easy.

Fortunately, for developers who need to use Windows features from within MATLAB, there is a simple method for calling them. MATLAB's language includes a function calllib, from which code in external simple C DLLs can be accessed.

MATLAB's calllib Function

C++
calllib( 'lib_name', 'function_name', arg1....argN )

The calllib function is a versatile feature built into the MATLAB language, which can pass multiple parameters to external libraries. To use calllib, first the DLL library needs to be opened using the loadlibrary function, and later needs to be closed using the unloadlibrary function:

C++
loadlibrary('MATLABconnector.dll', 'MATLABconnector.h')
calllib('MATLABconnector', 'fnMATLABconnector', 1)
unloadlibrary('MATLABconnector')

By placing the DLL, and its corresponding import .h file, in the same directory as the .m MATLAB file being developed, the loadlibrary function is kept simple; if it's located somewhere else, the calls to loadlibrary will have to specify the exact location of these files.

The calllib Parameters

  • Param 1: The name of the library to be called - this is the name of the DLL file, minus the .dll ending.
  • Param 2: The name of the function in the DLL file (in our example here, this is fnMATLABconnector, in practice this can be any name).
  • Param 3 and others: This can be one or more parameters, that are the parameters passed to the function we are calling (in our example, this is a simple integer).

The C DLL

This is a simple C-language DLL, comprising a DllMain function, and our function (or functions) that will be called from MATLAB. To be accessible from MATLAB, this must be compiled and built as a simple C language DLL, so that the function names are exported in a style that can be interpreted in MATLAB. (See http://en.wikipedia.org/wiki/Name_mangling for more information about name mangling, if you haven't heard of this before!). Source code can be found in the attached zip file for this example DLL - any function exported from a C DLL can be called though, so try experimenting with some different ones.

Our example considers a simple function to simulate posting WM_KEYDOWN and WM_KEYUP into the Windows message queue. This is a task easily accomplished from within C, which couldn't be done from within MATLAB. The third parameter in MATLAB's calllib function corresponds to the parameter in our fnMATLABconnector code, to specify which keycode will be used to send the WM_KEYDOWN and WM_KEYUP messages - in this example, we are using the left, right, up and down arrow keys.

Uses of this Technique

The project attached is a proof of concept only, and doesn't really have any real life applications as it is. To take this further, a combination of developing more advanced functions for the DLL, and basing the calls to it from within MATLAB using mathematically derived parameters is likely to be needed. For example, it would be possible to develop in C an application which controls the interface to a piece of hardware, and to simulate and drive it from within a MATLAB algorithm. This is an excellent way to cheaply develop and experiment with robotic control algorithms, etc. The only real limit to this technique is what you can dream up :-)

References

The Sample Files

Attached are the example files referred to here, with more detailed comments, and some project notes included. Please feel free to use the code however you wish - linking back to me (Chris Daw) if you want to credit a source.

History

  • 27th November, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
A long-time "lurker" on this site (that's what it calls me anyway!), I've just taken the plunge and written an article, on a subject I haven't found much on before when I've needed it. Hopefully it will be useful to a few people.

Comments and Discussions

 
Generalconnect some newly created dlls to the matlab Pin
E.Mageshwari22-Jan-10 19:59
E.Mageshwari22-Jan-10 19:59 
GeneralCalling a dll in matlab ! [modified] Pin
ahad44524-Jun-07 20:37
ahad44524-Jun-07 20:37 
GeneralDLL for Matlab Pin
maritkap5-Dec-06 22:48
maritkap5-Dec-06 22:48 
GeneralReinventing the wheel Pin
BAIJUMAX28-Nov-06 5:25
professionalBAIJUMAX28-Nov-06 5:25 
GeneralRe: Reinventing the wheel Pin
chrisdaw29-Nov-06 10:29
chrisdaw29-Nov-06 10:29 
... which is a great technique in some situations, but does require plenty of programming work, can be difficult to debug on some occasions, and is very poor in high-throughput real-time applications. MATLAB can also be a cross-platform tool, and my intention is to cover linking it with other systems where COM-type functionality is not available in a future article.

FYI - MATLAB COM is actually reinventing the wheel, as calllib has been around in different forms for a good deal longer Smile | :)
GeneralMATLAB Generic Dll Pin
Abbas_Riazi27-Nov-06 19:51
professionalAbbas_Riazi27-Nov-06 19:51 
GeneralRe: MATLAB Generic Dll Pin
chrisdaw29-Nov-06 10:22
chrisdaw29-Nov-06 10:22 
GeneralRe: MATLAB Generic Dll Pin
Abbas_Riazi29-Nov-06 10:32
professionalAbbas_Riazi29-Nov-06 10:32 

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.