|
|||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionAs all of you know, MATLAB is a powerful engineering language. Because of some limitation, some tasks take very long time to proceed. Also MATLAB is an interpreter not a compiler. For this reason, executing a MATLAB program (m file) is time consuming. For solving this problem, Mathworks provides us C Math Library or in common language, MATLAB API. A developer can employ these APIs to solve engineering problems very fast and easy. This article is about how can use these APIs. MATrix LABoratoryMATLAB is abbreviation of Matrix Laboratory. This means every computation was performed in matrix form. In other hand every data type wrapped in matrix form and every functions take these matrix as input argument. A = (3x2 + 5x + 7) (4x5 + 3x3 - x2 + 1) You can use two matrices for coefficients of any polynomials: [3 5 7] for (3x2 + 5x + 7) and [4 0 3 -1 0 1] for (4x5 + 3x3 - x2 + 1), using conv function, we can obtain coefficients of result: conv([3 5 7], [4 0 3 -1 0 1]): A = [12 20 37 12 16 -4 5 7] means: A= 12x7 + 20x6 + 37x5 + 12x4 + 16x3 - 4x2 + 5x + 7 C Math LibraryThe functions fall into two groups: the mathematical functions and the utility functions. We use mathematical functions for computing and utility functions for constructing an array or matrix or printing content of a matrix. Every matrices represented by One C prototype supports all the possible ways to call a particular MATLAB C Math Library function. You can reconstruct the C prototype by examining the MATLAB syntax for a function. In the following procedure, the MATLAB function svd() and the corresponding library function mlfSvd() are used to illustrate the process. MATLAB Syntaxs = svd (X) The C prototype for mlfSvd() is constructed step-by-step. Until the last step, the prototype is incomplete. Adding the Output Arguments1- Find the statement that includes the largest number of output arguments. [U, S, V] = svd (X, 0) 2- Subtract out the first output argument, U, to be the return value from the function. The data type for the return value is mxArray*. mxArray* mlfSvd( 3- Add the remaining number of MATLAB output arguments, S and V, as the first, second, etc., arguments to the C function. The data type for a C output argument is mxArray**. mxArray* mlfSvd(mxArray **S, mxArray **V Adding the Input Arguments1- Find the syntax that includes the largest number of input arguments. [U, S, V] = svd (X, 0) 2- Add that number of input arguments, X and Zero, to the prototype, one after another following the output arguments. The data type for an input argument is mxArray*. mxArray* mlfSvd (mxArray** S, mxArray** V, mxArray* X, mxArray* Zero); The prototype is complete. How to Translate a MATLAB Call into a C CallThis procedure demonstrates how to translate the MATLAB svd() calls into MATLAB C Math Library calls to mlfSvd(). The procedure applies to library functions in general.
The MATLAB arguments to svd() fall into these categories: 1- Declare input, output, and return variables as mxArray* variables, and assign values to the input variables. s = mlfSvd (NULL, NULL, 4- Pass any required or optional input arguments that apply to the C function, following the output arguments. Pass a NULL argument wherever an optional input argument does not apply to the particular call. s = mlfSvd (NULL, NULL, X, NULL); Mathematical FunctionsEvery mathematical functions are begin with mlf prefix. mlf is an abbreviation for MATLAB Function. Below is a list of useful mathematical functions:
For example, Conv statement in MATLAB will become mlfConv in C. Utility FunctionsWe use utility functions for some tasks like printing content of a matrix or saving/loading data to/from a file. Every utility functions are begin with mx prefix. Below is a list of some utility functions:
Using C Math LibraryTo add support of MATLAB C Math Library follow these instructions:
matlab.h is interface of MATLAB APIs. Add directory of MATLAB interface files (*.h) to Visual Studio (Tools -> Options -> Directories). For example: x:\matlab\extern\include where x is drive letter of matlab path. 2- Add desired libraries to your project (In this example, libmat.lib, libmx.lib, libmatlbmx.lib and libmatlb.lib) 3- Compile your project! Sample Program
Requirements1- MATLAB v5.0 or higher 2- MATLAB C Math Library Toolbox 3- Knowledge of MATLAB programming! References1- MATLAB C Math Library (Mathworks) 2- C Math Library Reference (Mathworks) Enjoy!
|
||||||||||||||||||||||||||||||||||||||||||||