11,640,125 members (67,661 online)

# Solving Engineering Problems Using MATLAB C API

, 23 May 2003 507.3K 5.3K 80
 Rate this:
Using MATLAB engine to solve engineering problems.
<!-- Article image -->

<!-- Add the rest of your HTML here -->

## Introduction

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

MATLAB 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.
For example you want to multiply to polynomial as follow:

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 Library

The 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 `mxArray` a data type introduced by MATLAB for constructing a matrix. As I said before, every data must be wrapped in a matrix form in other hand: `mxArray`

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 Syntax

s = svd (X)
[U, S, V] = svd (X)
[U, S, V] = svd (X, 0)

The C prototype for mlfSvd() is constructed step-by-step. Until the last step, the prototype is incomplete.

### Adding the Output Arguments

1- Find the statement that includes the largest number of output arguments.
Choose:

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

1- Find the syntax that includes the largest number of input arguments.
Choose:

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

This 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.
Note that within a call to a MATLAB C Math Library function, an output argument is preceded by &, an input argument is not.
MATLAB Syntax:

s = svd (X)
[U, S, V] = svd (X)
[U, S, V] = svd (X, 0)

The MATLAB arguments to svd() fall into these categories:
U (or s) is a required output argument.
S and V are optional output arguments.
X is a required input argument.
Zero is an optional input argument.

1- Declare input, output, and return variables as mxArray* variables, and assign values to the input variables.
2- Make the first output argument the return value from the function.
s =
U =
U =
3- Pass any additional required or optional output arguments as the first arguments to the function. Pass a NULL argument wherever an optional output argument does not apply to the particular call.

s = mlfSvd (NULL, NULL,
U = mlfSvd(&S, &V,
U = mlfSvd(&S, &V,

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);
U = mlfSvd (&S, &V, X, NULL);
U = mlfSvd (&S, &V, X, Zero);

## Mathematical Functions

Every mathematical functions are begin with mlf prefix. mlf is an abbreviation for MATLAB Function. Below is a list of useful mathematical functions:

 mlfPlus, mlfMinus mlfMtimes, mlfMpower mlfAcos, mlfAsin mlfConv mlfConj mlfDec2bin, mlfDec2hex mlfDisp mlfFft, mlfFft2 mlfLinspace mlfMax, mlfMin mlfRoots mlfRot90

For example, Conv statement in MATLAB will become mlfConv in C.

## Utility Functions

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

 mxCalloc, mxFree mxCreateDoubleMatrix mxCreateNumericArray mxCreateString mxGetPi, mxGetPr mxMalloc, mxRealloc mxGetData, mxSetData mxDestroyArray

## Using C Math Library

To add support of MATLAB C Math Library follow these instructions:
1- Add following line at the end of stdafx.h

`<P>#include <span class="code-keyword"><matlab.h></P></span>`

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

```<P>#include <span class="code-string">"stdafx.h"</P>
</span>
<P>
int main(int argc, char* argv[])
{
double dblArray[]={1, 2, 3, 4, 5, 6, 7, 8, 9}; mxArray *A, *B;

A=mxCreateDoubleMatrix(3, 3, mxREAL);</P><P></P><P>	//copy array to matrix A</P><P>	memcpy(mxGetPr(A), dblArray, 9 * sizeof(double));</P><P> </P><P>	A=mlfMTimes(A, A);   //A=A.^2;</P><P>	mlfPrintMatrix(A);

//Creating Magic Matrix</P><P>	B=mlfMagic(mlfScalar(3)); //Magic matrix of order 3 mlfPrintMatrix(B); mxDestroyArray(A); mxDestroyArray(B);

return 0;
} </P>```

## Requirements

1- MATLAB v5.0 or higher

2- MATLAB C Math Library Toolbox

3- Knowledge of MATLAB programming!

## References

1- MATLAB C Math Library (Mathworks)

2- C Math Library Reference (Mathworks)

Enjoy!

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

## About the Author

 CEO Misbah3Com Iran (Islamic Republic Of)
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 Acqusition 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 competetion, my articles are:

You can see list of my articles, by clicking here

## Comments and Discussions

 First PrevNext
 My vote of 5 manoj kumar choubey26-Feb-12 19:51 manoj kumar choubey 26-Feb-12 19:51
 Mxarray to double or int golnazbaghdadi2-Mar-11 12:13 golnazbaghdadi 2-Mar-11 12:13
 using matlab in c++ s_mahdavi3-Nov-09 7:24 s_mahdavi 3-Nov-09 7:24
 Where can I find matlab.h? Member 443275510-Aug-09 21:13 Member 4432755 10-Aug-09 21:13
 Object and UDP combination problem vaibhav_gandhi15-Jan-09 6:33 vaibhav_gandhi 15-Jan-09 6:33
 #include BerakiRuth27-Oct-08 11:08 BerakiRuth 27-Oct-08 11:08
 convert c file to mfile Member 399935931-May-08 13:09 Member 3999359 31-May-08 13:09
 help! sanfrea28-May-08 1:12 sanfrea 28-May-08 1:12
 hi.. im a beginner to all this and have a number of questions.. first off.. i have a face recognition code which we've written in matlab.. and we hafta convert the code to C.. in that code is the svd function which we want to call in c.. so we'll hafta call that using the matlab c/c++ library from wat ive read.. now.. installing the matlab c library.. wats the procedure for that...secondly..wat would be the steps to follow? ur help in the matter would greatlyyyyy appreciated.. thanks in advance!
 plz give me quick reply uday divekar7-May-08 0:53 uday divekar 7-May-08 0:53
 Amazing problem smzhaq18-Jul-07 0:15 smzhaq 18-Jul-07 0:15
 Cygwin molakey22-Jun-07 0:51 molakey 22-Jun-07 0:51
 Testing the demo molakey19-Jun-07 1:43 molakey 19-Jun-07 1:43
 Cannot find Matlab.h molakey19-Jun-07 1:05 molakey 19-Jun-07 1:05
 Using Matlab C functions from cygwin and Linux molakey19-Jun-07 0:42 molakey 19-Jun-07 0:42
 MATLAB Compiler include the Matlab C Math Library, but how to use it? patiobarbecue13-Jun-07 9:06 patiobarbecue 13-Jun-07 9:06
 C to Matlab convertor famot30-May-07 1:19 famot 30-May-07 1:19
 Re: C to Matlab convertor karunakaran g n23-Apr-08 0:00 karunakaran g n 23-Apr-08 0:00
 Calling a Matlab function from VB sumaap22-Feb-07 0:22 sumaap 22-Feb-07 0:22
 Re: Calling a Matlab function from VB A. Riazi23-Feb-07 8:42 A. Riazi 23-Feb-07 8:42
 please help me for god sake melika_13601-Feb-07 4:36 melika_1360 1-Feb-07 4:36
 matlab help??? ersincakir4-Jan-07 15:26 ersincakir 4-Jan-07 15:26
 using MATLAB graphics from vc++ Sajjad Haider14-Nov-06 2:28 Sajjad Haider 14-Nov-06 2:28
 Re: using MATLAB graphics from vc++ A. Riazi14-Nov-06 3:48 A. Riazi 14-Nov-06 3:48
 mx GetPr barbu8127-Oct-06 4:49 barbu81 27-Oct-06 4:49
 Re: mx GetPr satish2deee14-Apr-08 20:04 satish2deee 14-Apr-08 20:04
 Calling C function file by Matlab ODE solver javaid_phd30-Sep-06 18:53 javaid_phd 30-Sep-06 18:53
 How can I get matlah.h file? S. M. Kamrul Hasan16-Jul-06 4:28 S. M. Kamrul Hasan 16-Jul-06 4:28
 Re: How can I get matlah.h file? A. Riazi16-Jul-06 7:34 A. Riazi 16-Jul-06 7:34
 Re: How can I get matlah.h file? Sindhu Krishnana E P10-May-07 2:49 Sindhu Krishnana E P 10-May-07 2:49
 Re: How can I get matlah.h file? molakey19-Jun-07 1:40 molakey 19-Jun-07 1:40
 Urgent! How can I upload my model file? longwang3-Jul-06 17:58 longwang 3-Jul-06 17:58
 Urgent! Help : Running real time target model cause computer reboot! longwang2-Jul-06 16:41 longwang 2-Jul-06 16:41
 Thanks(I am able to read mat file now but new problem... ) Agrawalg2-Jun-06 3:18 Agrawalg 2-Jun-06 3:18
 linker error euskalboy25-May-06 23:25 euskalboy 25-May-06 23:25
 problem(i don't have libmatlbmx.lib and libmatlb.lib file ) Agrawalg9-May-06 19:47 Agrawalg 9-May-06 19:47
 Re: problem(i don't have libmatlbmx.lib and libmatlb.lib file ) A. Riazi10-May-06 6:38 A. Riazi 10-May-06 6:38
 Re: problem(i don't have libmatlbmx.lib and libmatlb.lib file ) Agrawalg15-May-06 2:22 Agrawalg 15-May-06 2:22
 Re: problem(i don't have libmatlbmx.lib and libmatlb.lib file ) A. Riazi17-May-06 3:55 A. Riazi 17-May-06 3:55
 Re: problem(i don't have libmatlbmx.lib and libmatlb.lib file ) sprmat847-Jul-06 7:39 sprmat84 7-Jul-06 7:39
 Re: problem(i don't have libmatlbmx.lib and libmatlb.lib file ) Arif W.P.5-Oct-07 1:56 Arif W.P. 5-Oct-07 1:56
 Re: problem(i don't have libmatlbmx.lib and libmatlb.lib file ) molakey19-Jun-07 1:37 molakey 19-Jun-07 1:37
 Using mxArrays in C# ncysnero5-May-06 12:11 ncysnero 5-May-06 12:11
 How to access cellarrays Waetzold28-Mar-06 3:28 Waetzold 28-Mar-06 3:28
 Re: How to access cellarrays momow7-Nov-06 20:05 momow 7-Nov-06 20:05
 Translate Matlab to C/C++ C6J7203-Jan-06 21:46 C6J720 3-Jan-06 21:46
 how to use matlab in c++ nmaleki6-Nov-05 17:49 nmaleki 6-Nov-05 17:49
 Re: how to use matlab in c++ A. Riazi6-Nov-05 17:56 A. Riazi 6-Nov-05 17:56
 Re: how to use matlab in c++ Sindhu Krishnana E P10-May-07 2:53 Sindhu Krishnana E P 10-May-07 2:53
 I have a Compiling error C2065: 'memcpy' : undeclared identifier ccwu_tuta8-Sep-05 23:08 ccwu_tuta 8-Sep-05 23:08
 Re: I have a Compiling error C2065: 'memcpy' : undeclared identifier A. Riazi9-Sep-05 19:52 A. Riazi 9-Sep-05 19:52
 Last Visit: 31-Dec-99 18:00     Last Update: 31-Jul-15 17:23 Refresh 1234 Next »

General    News    Suggestion    Question    Bug    Answer    Joke    Rant    Admin

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