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

Connecting MATLAB to C-language DLLs

, 27 Nov 2006
Rate this:
Please Sign up or sign in to vote.
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

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 Smile | :)

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
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

 
GeneralMATLAB Generic Dll PinmemberA. Riazi27-Nov-06 19:51 
GeneralRe: MATLAB Generic Dll Pinmemberchrisdaw29-Nov-06 10:22 
GeneralRe: MATLAB Generic Dll PinmemberA. Riazi29-Nov-06 10:32 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 27 Nov 2006
Article Copyright 2006 by chrisdaw
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid