Examples of mixed programming between VC and Matlab (the function of MATLAB 7.0 is converted to the. dll of Vc)

Source: Internet
Author: User

From http://goodenoughcui.diandian.com/post/2011-05-22/40033743170

First, it is a book called C language and Matlab interface: programming and examples. It mainly talks about how to call C-Mex functions from MATLAB ), it introduced that MATLAB is a fully functional integrated environment for programming and data processing. It provides a large number of powerful built-in functions and toolboxes to complete almost all tasks. In addition, MATLAB provides well-functional APIs to communicate with external programs or other languages.

Although, what I care about most is not how MATLAB calls C language, but how to let C call Matlab. However, the detailed introduction in this book gives me a certain understanding of mixed encoding, such as the mxarray struct, the sequential access of Matrix Elements in C and Matlab (C by line, matlab by column), data interaction between C and MATLAB, and memory release.

The second good article is "quick implementation of VC ++ and Matlab Mixed Programming". This is a paper written by a buddy of Jilin University. It mainly talks about how to implement it. compile the M file. DLL files are put into the VC ++ program. This method is practical and simple:

1. Set the MATLAB compiling environment and find the VC Compiler (I used the MATLAB, vs. net2003, or msvc7.1)

2. Create a dynamic link library and use the MCC command (for example, for my example below): MCC-W cpplib: libinverse-T link: Lib inverse. M.

3. Use it in. DLL. First, you must configure environment variables for vs (for more specific configurations, see environment), including system, VS, and project configurations, each time a new project is generated, the project configuration should be performed on it (VC ++ 6 is different from vs2003 configuration, mainly because the location of some options may be different, you can refer to the configuration made when you connect to MySQL in the front, which is similar to the setting position ).

After the configuration is complete, the front side is generated. CTF. DLL. h. lib (The following example is libinverse. CTF, libinverse. DLL, libinverse. hlibinverse. lib) file into the main directory of the VC program, put libinverse. h. DLL (you only need to add # include "libinverse. H ").

A specific example is provided:

In Matlab, I wrote a simple inverse. m implementation to find the inverse of the matrix:

Functiony = inverse (A, num)

% Num is the dimension of a, which has no practical significance.

Y = inv ();

 

VC call:

 

// Test_matlab .cpp: defines the entry point for the consoleapplication.

// The program contains some other tests I have done, such as the write sequence of the memory and how to transmit parameters.

# Include "stdafx. H"

# Include "mclmcr. H" // mxarray type declaration

# Include "libinverse. H" // DLL header file

Intmain ()

{

// Initialize the program

Int I, J;

If (! Mclinitializeapplication (null, 0 ))

{

Fprintf (stderr, "cocould not initialize theapplication. \ n ");

Exit (1 );

}

// Initialize the database

If (! Libinverseinitialize ())

{

Fprintf (stderr, "cocould not initialize the library. \ n ");

Exit (1 );

}

Printf ("game over! \ N ");

Int num = 2600;

Double ** xx = new double * [num];

XX [0] = new double [num * num];

For (I = 0; I <num; I ++)

{

XX [I] = XX [0] + I * num;

}

Double * x = new double [num * num];

For (I = 0; I <num; I ++)

For (j = 0; j <num; j ++)

{

If (I = J)

XX [I] [J] = 2;

Else

XX [I] [J] = 0;

}

For (I = 0; I <num; I ++)

For (j = 0; j <num; j ++)

{

X [I * num + J] = XX [J] [I]; // a column in MATLAB

}

Mxarray * n; // input variable

Mxarray *;

Mxarray * Y = NULL; // output variable

N = mxcreatescalardouble (Num); // create a scalar

A = mxcreatedoublematrix (Num, num, mxreal); // allocate memory

Memcpy (mxgetpr (N), & num, sizeof (INT); // note that the pointer type & num is used to assign values to the mxarray object.

Memcpy (mxgetpr (A), X, num * sizeof (double); // data interaction between VC and Matlab converts int to mxarray type

Printf ("beginning \ n ");

Mlfinverse (1, & Y, A, n); // the declaration of this function in the header file is: extern voidmlfinverse (INT nargout, mxarray ** y, mxarray *, mxarray * num); where nargout is the number of output parameters, ** Y is the output parameter, and the other two are the input parameters.

Printf ("ending! \ N ");

Mxdestroyarray ();

Double * data;

Double ** Mm = new double * [num];

Mm [0] = new double [num * num];

For (I = 0; I <num; I ++)

{

Mm [I] = mm [0] + I * num;

}

Data = mxgetpr (y );

Int R, C;

R = mxgetm (y); // number of rows = num

C = mxgetn (y); // Number of columns = num

For (I = 0; I <r; I ++)

 

{

For (j = 0; j <C; j ++)

{

Mm [I] [J] = data [J * r + I]; // note that the column and column are inverted.

// Printf ("% 4.2f", Mm [I] [J]);

}

// Printf ("\ n ");

}

// Mxfree (y); // mxfree the mxdestroyarray (y) cannot be used here, because it can only be used to release the memory occupied by the mxarray array.

Mxfree (N );

// Mxdestroyarray (N );

Mxdestroyarray (y); // although y is not created, it allocates memory during function calling (because it is a matrix), so it must be released or memory leakage may occur.

Libinverseterminate (); // close the library and program

Mclterminateapplication ();

Delete [] X;

Delete [] Mm [0];

Delete [] mm;

Delete [] XX [0];

Delete [] xx;

}

It can be seen that VC calls MATLAB not only improves the program running speed, but also has a more important significance that we can use this method to directly call the numerical operation functions in MATLAB in later programming, you don't have to write it manually, and you don't have to worry about the accuracy and speed of your program.

However, the question behind is how the program can be carried out in a environment without Matlab. I have not carefully tested it, and there is still a problem that php7.0 does not support vs2005 (msvc8, how to combine the two is indeed a problem, but you can choose to use the combination of MATLAB r2007b and vs2005, and r2007b seems to have the multi-thread operation function, now multi-core is so popular, If you can use multi-thread computing, the hardware is fully utilized to improve the computing efficiency.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.