Click here to Skip to main content
15,888,968 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 443.8K   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

 
QuestionRe: plz!! HELP!!! call DLL from MATLAB Pin
RadDeveloper26-Feb-07 6:23
RadDeveloper26-Feb-07 6:23 
QuestionCan I take "Ogre" into MATLAB? Pin
Kamran Bigdely-Shamloo12-Aug-06 4:03
Kamran Bigdely-Shamloo12-Aug-06 4:03 
AnswerRe: Can I take "Ogre" into MATLAB? Pin
Abbas_Riazi12-Aug-06 5:52
professionalAbbas_Riazi12-Aug-06 5:52 
GeneralLCC preprocessor error [modified] Pin
A.S.Nandagopalan12-Jul-06 11:21
A.S.Nandagopalan12-Jul-06 11:21 
QuestionI am confused :( [modified] Pin
areddy20067-Jul-06 5:20
areddy20067-Jul-06 5:20 
GeneralTo Mr Riazi, thanks a lot Pin
Sylvianne30-Jun-06 2:56
Sylvianne30-Jun-06 2:56 
AnswerRe: To Mr Riazi, thanks a lot Pin
Abbas_Riazi30-Jun-06 3:35
professionalAbbas_Riazi30-Jun-06 3:35 
QuestionCan i use these header files with loadlibrary? Pin
Sylvianne23-Jun-06 2:31
Sylvianne23-Jun-06 2:31 
Hi,
I am afraid my header file is not simple at all. I have to two that i should(since i have two classes) use i believe, how do i manage this?

It is a Filter DLL

Here is one header file FilterDLL.h

#include "afxwin.h"

class CFilterDLL : public CObject
{
public:
int GetMaxNoiseLevel();
char * GetErrorString(int errcode);
// void GetErrorString(int, char *);
double GetSensorSampleRate();
int GetOverSamplingFactor();
int SetOverSamplingFactor(int);
int SetCreisMode(int);
int SetOffset(int, double);
int GetOffset(int *, double *);
double GetOffsetRegulationTime();
int GetZeroOutput();
int GetOutputDataWidth();
int SetExtParam(int, double);
int GetExtParam(int, double *);
int SetNoiseParam(int, int);
int GetNoiseParam(int *, int *);
double GetMaxSimuSensitivityTolerance();
int SetSensitivityTolerance(double);
double GetMaxSimuFilterTolerance();
int SetFilterFrequencyTolerance(double);
int GetFilterFrequencyTolerance(double *);
int GetSensitivityTolerance(double *);
double GetMaxFilterInputSampleRate();
int GetClippingElec(double *, double *);
double GetMaxFilterFrequencyTolerance();
double GetFilterFrequency();
double GetMaxSensitivityTolerance();
double GetSensitivity();
void GetChannelDescription(char *);
void GetSensorDescription(char *);
void GetDllFileName(char *);
int GetFilterVersion();
long GetOutputDataSampleCount();
double GetOutputDataSampleRate();
int GetChannelNumber();
int SetFilterType (char*);
int GetInterfaceVersion();
int SetSyncMode(int);
int GetSyncMode();
int SetDevice(int, int);
int AddDevice();
int DeleteDevice(int);
int GetClippingMech(double*, double*, double*);
int SetInputData(double*, long, double);
int Calculate();
int GetOutputData(short*);
int GetSensorValue(double, double, double*);
int SetLimitsClippingMech(double, double);
int GetLimitsClippingMech(double *, double *);

CFilterDLL();
virtual ~CFilterDLL();

protected:
bool CalculateTranferFunction();
bool FreeDLL();
void ClearProcPointer();
bool GetProcAddresses();
int m_iInterfaceVersion;

typedef int (__cdecl *BASICPROC_0) (double*, double*);
typedef int (__cdecl *BASICPROC_1) (int, double);
typedef double (__cdecl *BASICPROC_2) ();
typedef int (__cdecl *BASICPROC_3) (int);
typedef int (__cdecl *BASICPROC_4) (double);
typedef int (__cdecl *BASICPROC_5) (char*);
typedef void (__cdecl *BASICPROC_6) (char*);
typedef int (__cdecl *BASICPROC_7) ();
typedef int (__cdecl *BASICPROC_8) (int, int);
typedef void (__cdecl *BASICPROC_9) ();
typedef int (__cdecl *BASICPROC_10) (double*, double*, double*);
typedef int (__cdecl *BASICPROC_11) (double*, long, double);
typedef int (__cdecl *BASICPROC_12) (double*);
typedef int (__cdecl *BASICPROC_13) (double, double*);
typedef int (__cdecl *BASICPROC_14) (double, double);
typedef long (__cdecl *BASICPROC_15) ();
typedef int (__cdecl *BASICPROC_16) (int*, int*);
typedef int (__cdecl *BASICPROC_17) (short*);
typedef void (__cdecl *BASICPROC_18) (int, char*);
typedef int (__cdecl *BASICPROC_19) (double, double, double*);
typedef int (__cdecl *BASICPROC_20) (int*, double*);
typedef int (__cdecl *BASICPROC_21) (int, double*);
typedef char * (__cdecl *BASICPROC_22) (int);

HINSTANCE hDLL;

BASICPROC_7 m_ProcGetMaxNoiseLevel;
BASICPROC_7 m_ProcGetZeroOutput;
BASICPROC_7 m_ProcGetOutputDataWidth;
BASICPROC_1 m_ProcSetExtParam;
BASICPROC_21 m_ProcGetExtParam;
BASICPROC_8 m_ProcSetNoiseParam;
BASICPROC_16 m_ProcGetNoiseParam;
BASICPROC_2 m_ProcGetMaxSimuSensitivityTolerance;
BASICPROC_4 m_ProcSetSensitivityTolerance;
BASICPROC_2 m_ProcGetMaxSimuFilterTolerance;
BASICPROC_4 m_ProcSetFilterFrequencyTolerance;
BASICPROC_12 m_ProcGetFilterFrequencyTolerance;
BASICPROC_12 m_ProcGetSensitivityTolerance;
BASICPROC_2 m_ProcGetMaxFilterInputSampleRate;
BASICPROC_0 m_ProcGetClippingElec;
BASICPROC_2 m_ProcGetMaxFilterFrequencyTolerance;
BASICPROC_2 m_ProcGetFilterFrequency;
BASICPROC_2 m_ProcGetMaxSensitivityTolerance;
BASICPROC_2 m_ProcGetSensitivity;
BASICPROC_6 m_ProcGetChannelDescription;
BASICPROC_6 m_ProcGetSensorDescription;
BASICPROC_6 m_ProcGetDllFileName;
BASICPROC_7 m_ProcGetFilterVersion;
BASICPROC_15 m_ProcGetOutputDataSampleCount;
BASICPROC_2 m_ProcGetOutputDataSampleRate;
BASICPROC_7 m_ProcGetChannelNumber;
BASICPROC_7 m_ProcGetInterfaceVersion;
BASICPROC_3 m_ProcSetSyncMode;
BASICPROC_7 m_ProcGetSyncMode;
BASICPROC_8 m_ProcSetDevice;
BASICPROC_7 m_ProcAddDevice;
BASICPROC_3 m_ProcDeleteDevice;
BASICPROC_10 m_ProcGetClippingMech;
BASICPROC_11 m_ProcSetInputData;
BASICPROC_7 m_ProcCalculate;
BASICPROC_17 m_ProcGetOutputData;
BASICPROC_19 m_ProcGetSensorValue;
BASICPROC_14 m_ProcSetLimitsClippingMech;
BASICPROC_0 m_ProcGetLimitsClippingMech;
BASICPROC_1 m_ProcSetOffset;
BASICPROC_20 m_ProcGetOffset;
BASICPROC_2 m_ProcGetOffsetRegulationTime;
BASICPROC_3 m_ProcSetCreisMode;
// BASICPROC_3 m_ProcSetSampleVari;
BASICPROC_3 m_ProcSetOverSamplingFactor;
BASICPROC_7 m_ProcGetOverSamplingFactor;
// BASICPROC_3 m_ProcSetOutputVariation;
// BASICPROC_7 m_ProcGetOutputVariation;
BASICPROC_2 m_ProcGetSensorSampleRate;
// BASICPROC_18 m_ProcGetErrorString;
BASICPROC_22 m_ProcGetErrorString;
The second header file is OutputTooling.h

class COutputTooling
{
public:
void SaveBuffer(short *psBuffer, double dSampleRate, long lSampleCount, CString strText);
void SaveBuffer(double *pdBuffer, double dSampleRate, long lSampleCount, CString strText, int idig);
COutputTooling();
virtual ~COutputTooling();

};

I hope i won't have to rewrite the whole thing using MEX files.
I am not familiar with DLL, that's why i may seem upset

I actually work with GUI, i should be able to implement a callback to a Filter push button to filter some files and plot them.

Sorry for the mess, i hope not to take too much of your time.

Thx a lot

Generalmbuild -setup Pin
Sylvianne20-Jun-06 21:31
Sylvianne20-Jun-06 21:31 
GeneralRe: mbuild -setup Pin
Sylvianne20-Jun-06 21:33
Sylvianne20-Jun-06 21:33 
AnswerRe: mbuild -setup Pin
Abbas_Riazi23-Jun-06 0:20
professionalAbbas_Riazi23-Jun-06 0:20 
GeneralRe: mbuild -setup Pin
Sylvianne23-Jun-06 2:33
Sylvianne23-Jun-06 2:33 
GeneralWrapper DLL to load DLL in Matlab Pin
Sylvianne19-Jun-06 1:35
Sylvianne19-Jun-06 1:35 
AnswerRe: Wrapper DLL to load DLL in Matlab Pin
Abbas_Riazi19-Jun-06 21:45
professionalAbbas_Riazi19-Jun-06 21:45 
GeneralRe: Wrapper DLL to load DLL in Matlab Pin
Sylvianne19-Jun-06 21:52
Sylvianne19-Jun-06 21:52 
AnswerRe: Wrapper DLL to load DLL in Matlab Pin
Abbas_Riazi19-Jun-06 23:41
professionalAbbas_Riazi19-Jun-06 23:41 
GeneralRe: Wrapper DLL to load DLL in Matlab Pin
Sylvianne20-Jun-06 4:14
Sylvianne20-Jun-06 4:14 
GeneralRe: Wrapper DLL to load DLL in Matlab Pin
Sylvianne19-Jun-06 21:57
Sylvianne19-Jun-06 21:57 
GeneralRe: Wrapper DLL to load DLL in Matlab Pin
Sylvianne19-Jun-06 22:25
Sylvianne19-Jun-06 22:25 
GeneralProblem with addheader (load DLL) Pin
Sylvianne16-Jun-06 3:29
Sylvianne16-Jun-06 3:29 
GeneralDoes Not Compile Pin
Guillochon25-Apr-06 9:22
Guillochon25-Apr-06 9:22 
GeneralCalling MFC Dll in Matlab Pin
Wahaj Khan14-Apr-06 23:57
Wahaj Khan14-Apr-06 23:57 
AnswerRe: Calling MFC Dll in Matlab Pin
Abbas_Riazi15-Apr-06 0:44
professionalAbbas_Riazi15-Apr-06 0:44 
GeneralRe: Calling MFC Dll in Matlab Pin
Wahaj Khan17-Apr-06 0:03
Wahaj Khan17-Apr-06 0:03 
AnswerRe: Calling MFC Dll in Matlab Pin
Abbas_Riazi18-Apr-06 4:42
professionalAbbas_Riazi18-Apr-06 4:42 

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.