Movatterモバイル変換


[0]ホーム

URL:


Wayback Machine
41 captures
13 Dec 2007 - 16 Jun 2023
NovJANFeb
Previous capture06Next capture
201020122013
success
fail
COLLECTED BY
Organization:Alexa Crawls
Starting in 1996,Alexa Internet has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to theWayback Machine after an embargo period.
Collection:Alexa Crawls
Starting in 1996,Alexa Internet has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to theWayback Machine after an embargo period.
TIMESTAMPS
loading
The Wayback Machine - https://web.archive.org/web/20120106201244/http://www.codeproject.com:80/KB/cpp/matlab_c_api.aspx
Click here to Skip to main content
8,389,339 members and growing!
EmailPassword Lost password?
Home
Search within:




Licence 
First Posted 23 May 2003
Views 417,891
Downloads 4,741
Bookmarked 69 times

Solving Engineering Problems Using MATLAB C API

ByA. Riazi | 23 May 2003
Using MATLAB engine to solve engineering problems.
Prize winner in Competition "MFC/C++ Apr 2003"
 
See Also
Print Article
add
Add to your CodeProject bookmarks
Discuss
Discuss this article
176
  4.90 (44 votes)
4 votes, 9.1%
1
1 vote, 2.3%
2
1 vote, 2.3%
3
3 votes, 6.8%
4
35 votes, 79.5%
5
4.90/5 - 44 votes
5 removed
μ 4.53, σa 2.21 [?]
Sponsored Links

Solving engineering problem using MATLAB

Introduction

As all of you know,MATLAB is a powerful engineering language. Because of some limitation, some tasks take very long time to proceed. AlsoMATLAB 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), usingconv 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 bymxArray a data type introduced byMATLAB 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 particularMATLAB C Math Library function. You can reconstruct the C prototype by examining theMATLAB syntax for a function. In the following procedure, theMATLAB functionsvd() and the corresponding library functionmlfSvd() 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 formlfSvd() 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 ismxArray*.

mxArray* mlfSvd(

3- Add the remaining number ofMATLAB output arguments, S and V, as the first, second, etc., arguments to the C function. The data type for a C output argument ismxArray**.

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 ismxArray*.

mxArray* mlfSvd (mxArray** S, mxArray** V, mxArray* X, mxArray* Zero);

The prototype is complete.

This procedure demonstrates how to translate theMATLABsvd() calls intoMATLAB C Math Library calls tomlfSvd(). 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)
 

TheMATLAB arguments tosvd() 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 asmxArray* 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 withmlfprefix. mlf is an abbreviation for MATLAB Function. Below is a list of useful mathematical functions:

mlfPlus, mlfMinusmlfMtimes, mlfMpowermlfAcos, mlfAsinmlfConv
mlfConjmlfDec2bin, mlfDec2hexmlfDispmlfFft, mlfFft2
mlfLinspacemlfMax, mlfMinmlfRootsmlfRot90

For example,Conv statement inMATLAB will becomemlfConv 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 withmx prefix. Below is a list of some utility functions:

mxCalloc, mxFreemxCreateDoubleMatrixmxCreateNumericArraymxCreateString
mxGetPi, mxGetPrmxMalloc, mxReallocmxGetData, mxSetDatamxDestroyArray

Using C Math Library

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

#include<matlab.h>

matlab.h is interface ofMATLAB APIs. Add directory ofMATLAB 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

#include"stdafx.h"

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

//copy array to matrix A

memcpy(mxGetPr(A), dblArray, 9 * sizeof(double));

A=mlfMTimes(A, A); //A=A.^2;

mlfPrintMatrix(A);//Creating Magic Matrix

B=mlfMagic(mlfScalar(3)); //Magic matrix of order 3 mlfPrintMatrix(B); mxDestroyArray(A); mxDestroyArray(B);return0;}


Requirements

1-MATLAB v5.0 or higher

2-MATLAB C Math Library Toolbox

3- Knowledge ofMATLAB programming!

References

1- MATLAB C Math Library (Mathworks)

2- C Math Library Reference (Mathworks)

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 foundhere

About the Author

A. Riazi

Software Developer (Senior)
Misbah3Com
Iran (Islamic Republic Of) Iran (Islamic Republic Of)

Member
I was born inShiraz, 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 fromIran 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 clickinghere



loading...
Sign Up to vote  PoorExcellent
Add a reason or comment to your vote:x
Votes of 3 or less require a comment

Comments and Discussions

 
 RefreshFirstPrevNext
GeneralMxarray to double or intmembergolnazbaghdadi13:13 2 Mar '11  
Generalusing matlab in c++members_mahdavi8:24 3 Nov '09  
QuestionWhere can I find matlab.h?memberMember 443275522:13 10 Aug '09  
QuestionObject and UDP combination problemmembervaibhav_gandhi7:33 15 Jan '09  
General#includememberBerakiRuth12:08 27 Oct '08  
Generalconvert c file to mfilememberMember 399935914:09 31 May '08  
Generalhelp!membersanfrea2:12 28 May '08  
Generalplz give me quick replymemberuday divekar1:53 7 May '08  
GeneralAmazing problemmembersmzhaq1:15 18 Jul '07  
GeneralCygwinmembermolakey1:51 22 Jun '07  
Last Visit: 19:00 31 Dec '99     Last Update: 10:12 6 Jan '121234567891011Next »

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

Permalink |Advertise |Privacy |Mobile
Web04 |2.5.120106.1 |Last Updated 24 May 2003
Article Copyright 2003 by A. Riazi
Everything elseCopyright ©CodeProject, 1999-2012
Terms of Use
Layout:fixed|fluid

The Daily Insider

[8]ページ先頭

©2009-2025 Movatter.jp