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
calllib( 'lib_name', 'function_name', arg1....argN )
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
calllib('MATLABconnector', 'fnMATLABconnector', 1)
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_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_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 :-)
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.
- 27th November, 2006: Initial post
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.