MATLAB combines CC ++ to establish separate applications

zhaozj2021-02-16  48

MATLAB combines C / C to establish separate applications

Author: CodeHunter

MATLAB provides a rich mathematical library, coupled with the toolbox function of Matlab itself, and the rich third-party toolbox function, which greatly reduces the difficulty in handling the complex mathematical operation model.

When developing business software, if the MATLAB's function library is applied, we must make our development efficiency improve, and the high-efficiency, maintenanceability, and scalability of the MATLAB function library just adapt to the development needs of commercial software.

But when we release business software, it is impossible to give each user to install a MATLAB. How do you have an extremely expensive price? How do I use MATLAB to build an independent application? Two, two alternative methods will be described below in conjunction with a simple example.

This article will use Windows 2000 as a development platform, Borland C Builder6 (hereinafter referred to as BCB) as a development tool, combined with MATLAB 6.1 to explain the application of MATLAB functions.

1 Preparation

Matlab provides a developer toolbox for development software, using the function library it provides, you can develop our own procedures. The development environment of establishing Maltlab and BCB is our primary completion.

1.1 MATLAB's architecture provided by the library provided

First we have to understand the architecture of Matlab's library, such as Figure 1.1, so we can clearly know that the function we call is from where the function is coming.

User code

MATLAB M-File Math Library

Matlab APRARY

MATLAB MATH Build-in Library

MATLAB UTILITY LIBRARY

ANSI C LIBRARY

Matlab Graphics Library

MATLAB function library architecture (Figure 1.1)

1.2 Installation Configuration Development Environment

Step 1: Install MATLAB6.1.

You will get the required files after the installation is complete. In the / extern directory, we developed the library and header files that you need to use. Table 1.2 shows the directories used in the development and its role control table.

Directory Effect / EXTERN / INCLUDE C Header / EXTERN / INCLUDE / CPP C Head / EXTERN / LIB / WIN32 / Borland provides lib files provided by various versions of the lib files / extern / LIB / WIN32 / Microsoft provides example directory provided by LIB files provided by each version of MS Visual C and its role control table 1.2

Note: If you use the BCB6 you can use the LIB file below

Step 2: Configure the development environment of the BCB.

Suppose you have already installed Matlab6.1 and BCB, then open the Project Options dialog, add / extern / include this path in the interlude path in the Directories / Conditionals panel, add / extern / lib. / Win32 / Borland This path. Then create a project and add the following file to the project.

/extern/lib/win32/borland/bc54/libmatlb.lib

/extern/lib/win32/borland/bc54/libmatlbmx.lib

/extern/lib/win32/borland/bc54/libmex.lib /extern/lib/win32/borland/bc54/libmmfile.lib

/extern/lib/win32/borland/bc54/libmx.lib

If you use a graphic function, you have to join the following two files to the project.

/extern/lib/win32/borland/bc54/sgl.lib

/extern/lib/win32/borland/bc54/libmwsglm.lib

2 development project

After the above preparation, we already have a development environment, which will give two simple examples to explain how the C Math function and the function defined in the Toolbox and M files.

2.1 method of using C Math function

Create a new console program, the main program is as follows:

#include

#include

#include

#include "matlab.h" / * necessary header files * /

int main ()

{

Double Num1, NUM2;

MxArray * volatile factors1 = null; / * must define pointers to Maarray * /

MxArray * Volatile Factors2 = NULL;

MxArray * Volatile Common_Factors = NULL;

MLFENTERNEWCONText (0, 0); / * Enable the function of automatic memory management * /

Printf ("Enter a Number:");

Scanf ("% lf", & num1);

Printf ("Enter A Second Number:");

Scanf ("% lf", & num2;

MLFTRY / * Used to capture error messages * /

{

MLFASSIGN (& Factors1, MLFFactor); / * mlfscalar function converts Num1 into MaaRray, then call MLFFactors to generate Num1 prime factor * /

Mlfassign (& Factors2, MLFFactor (mlfscalar (num2)));

MLFASSIGN (& Common_Factors, / * copy the intersection of Factors1 and Factors2 to the Common_Factors array * /

MLFINTERSECT (NULL, NULL, FACTORS1, FACTORS2, NULL);

IF (mlftobool (mlfisempty (common_factors)) / * If there is no common shutter * /

Printf ("% 0.0LF and% 0.0LF is relatively prime / n",

Num1, Num2);

Else

{

Printf ("% 0.0LF AND% 0.0LF Share Common Factor (s):",

Num1, Num2);

MLFPrintMatrix (Common_Factors);

}

} / * End mlftry * /

MLFCATCH / * Displayed the captured error * /

{

MLFPRINTF ("in catch block: / n");

MLFPrintMatrix (Mlflasterr (NULL));

}

MLFENDCATCH

MXDESTROYARRAY (Factors1); / * Releases the MAARRAY array of MAARRAY (Factors2) by MLFASSIGN;

MXDESTROYARRAY (Common_Factors);

MLFRESTOREPREVIOUSCONTEXT (0, 0); / * Turn off automatic memory management * /

Return (exit_success);

}

Compile and run this procedure, the results are similar to the following output:

ENTER a Number: 333

ENTER A Second Number: 444

333 and 444 Share Common Factor (s): 3 37

Last

转载请注明原文地址:https://www.9cbs.com/read-22596.html

New Post(0)