Current location - Training Enrollment Network - Mathematics courses - Excuse me, which warrior can use C++ to realize the size () function in MATLAB?
Excuse me, which warrior can use C++ to realize the size () function in MATLAB?
Using MATLAB language C and C++ mathematical function library in Visual C++

The c, C++ mathematical function library of MATLAB development environment takes mwArray structure as the data core, and the C/C++ file converted from M file by mcc command can be directly used in Viusal C++ development environment, but the compiled c, C++ files are generally lengthy and the code readability is poor. On the basis of mastering the C, C++ math library, readers can write their own C/C++ files based on the C, C++math library of the application program interface (API) and use the mwArray structure, which can greatly optimize the program code, reduce the storage space of the program code, and improve the efficiency and speed of program code execution. In the following description, a simple example will be used to demonstrate how to use mcc command to create an exe file running independently of MATLAB platform.

Firstly, the M language of MATLAB is used to establish the M file with specific functions that users need. Here, for the sake of simplicity, an M file magicCreat.m is established to realize the function of Rubik's Cube array.

Function y = magicCreat(n)

Y = magic (n)

Then from% matlabroot% \ extern \ examples \ compiler (%matlabroot% is the installation path of MATLAB in the reader's computer system, in this paper, it is

c:\ Program Files \ MATLAB \ r 2006a \ extern \ examples \ compiler)

Copy main_for_lib.c and main _ for _ lib.h to the current working path so as to call the mcc compilation command. Then create a C language program that calls the magicCreat.m file, as shown below:

# contains "stdio.h"

# contains "math.h"

#include "libPkg.h" // Compile the established library header file.

main( int argc,char **argv)

{

mxArray * N; /* Input variable matrix pointer */

MxArray * R = NULL/* result matrix pointer */

int n; //Default M file input variable value

/* Get command line parameters. If the command line input is less than 2, the input parameter defaults to 5 */

if(argc & gt; = 2) {

n = atoi(argv[ 1]);

} Otherwise {

n = 5;

}

//Initialize MCR and libPkg function libraries.

mcinitializeapplication(NULL,0);

libpkginialize();

/* Get the value of the input parameter */

n = mxCreateDoubleScalar(n);

/* Call the compilation file mlfMagicCreat*/

mlfMagicCreat( 1,& ampr,N);

/* Free up memory space */

mxDestroyArray(N);

mxDestroyArray(R);

//end libPkg library and MCR

libPkgTerminate();

mclTerminateApplication();

}

Where libPkg.h is the library file compiled by mcc command. The file structure is clear. First of all, the program code contains the necessary related header files and the definition of libPkg.h file, the main program entry and variables. Then initialize MCR and libPkg function libraries, call the file mlfMagicCreat compiled by magicCreat.m, and store the call results in matrix R. Finally, release the memory space and end the libPkg library and MCR. It is a good programming habit to release memory space in time before the program ends. Save the program as magicCreatCFile.c, then compile the file with mcc command, and enter in the command line window of MATLAB:

MCC-W lib:libPkg-T link:exe magic creat magiccreatcfile . c main _ for _ lib . c

After the mcc command is compiled, it can be found that a magicCreat.exe file appears in the current path, which is an executable program file. Enter it in the MATLAB command line window:

& gt& gt! Magic creation

Extracting CTF file. This may take several seconds, depending on

The size of the application. please wait ...

... CTF file extraction completed.

y =

17 24 1 8 15

23 5 7 14 16

4 6 13 20 22

10 12 19 2 1 3

1 1 18 25 2 9

The program runs normally, because the number of input variables on the command line is less than 2, so the default value of input parameters in the M file is 5, and the program generates a Rubik's cube array of order 5. If you need to enter the second input parameter, you can enter it in the MATLAB command line window in the following ways:

& gt& gt! Magic Create3% creates a cubic array of order 3.

y =

8 1 6

3 5 7

4 9 2