Click here to Skip to main content
Click here to Skip to main content

Connecting MATLAB to C-language DLLs

By , 27 Nov 2006
 

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

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:

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)

About the Author

chrisdaw
United Kingdom United Kingdom
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalconnect some newly created dlls to the matlabmemberE.Mageshwari22 Jan '10 - 19:59 
GeneralCalling a dll in matlab ! [modified]memberahad44524 Jun '07 - 20:37 
GeneralDLL for Matlabmembermaritkap5 Dec '06 - 22:48 
GeneralReinventing the wheelmemberbaiju.km28 Nov '06 - 5:25 
GeneralRe: Reinventing the wheelmemberchrisdaw29 Nov '06 - 10:29 
GeneralMATLAB Generic DllmemberA. Riazi27 Nov '06 - 19:51 
GeneralRe: MATLAB Generic Dllmemberchrisdaw29 Nov '06 - 10:22 
GeneralRe: MATLAB Generic DllmemberA. Riazi29 Nov '06 - 10:32 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 27 Nov 2006
Article Copyright 2006 by chrisdaw
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid