Summary: M file translates to CC ++ language file, VC compilation

xiaoxiao2021-03-06  81

Fork said in THU DEC 4 00:56:56 2003:

Sender: on the Sand of the Sahara Desert, the letter area: matlab

Title: Summary: M file translates into C / C language file, VC compilation

Sending station: BBS Harbin Institute of Technology (Sat Oct 30 17:44:20 2004)

This is the previous article, but it has been collected by the moderator. It cannot be searched by the author name, so send it again.

To find it.

──────────────────────────────

-

MATLAB is very convenient, but sometimes some special applications require us to Matlab M format

Programs in the file are translated into a form of C / C and compile in the C / C compiler.

The text summarizes the general method.

It takes two cases, the first is that the function of the drawing is not involved in your M file; the second

It is necessary to use the drawing function. The following uses examples, respectively:

The first case:

1. Built a M file, the content is:

%%%%%%%%%%%%%%%%%%%%%%

Function Y = fork_1 (n)

y = 0;

For i = 1: N

y = y i;

end

%%%%%%%%%%%%%%%%%%%%%%

After saving, in the command window:

Enter: (format: mcc -t -l cpp -h file name)

MCC -T -L CPP -H fork_1

Then you will find two files for Fork_1.cpp and fork_1.hpp in your working directory.

2. Built a dialog-based MFC application in VC, named TestFork1, add one

Button, and add a button response function, the function content is described in the fifth step. Be formed

Two files are copied to the TestFork1 directory of the VC project.

3. Select: Project ---> Settings, select Input in the drop-down menu, select Input in the VC ---> Settings, optional list.

Add the contents listed in Appendix A in the object / library module, pay attention to the space to pass them open and ignore

Add the content listed in Appendix B in the library; select the property table C / C option, drop down the menu selection general,

Add the contents of the appendix C in the pre-processing program definition, the original content should be retained, and pay attention to the comma

Separate them. Select the precompiled headers option for the drop-down menu, select "Automatic Us

Compensation header, add stdafx.h, determined.

4. Select: Tools ---> Select, Properties page Select "Directory" in include files:

C: / matlab6p5 / extern / incrude

C: / matlab6p5 / extern / include / cpp

Note that according to your MATLAB's installation location, modify the above address accordingly.

Add in Library Files:

C: / matlab6p5 / extern / lib / win32

C: / Matlab6p5 / Extern / LIB / WIN32 / Microsoft / MSVC60

Note that according to your MATLAB's installation location, modify the above address accordingly.

5. Add the following header file to the file where the button response function is located:

......

#include "matlab.hpp"

#include "fork_1.hpp" ......

Function Response Code is:

INT I;

Mwarray n;

N = 5;

n = fork_1 (n);

i = n.extractscalar (1);

CString Str;

Str.Format ("% d", i);

AfxMessageBox (STR);

6. Compile, connect, execute, everything OK! (Note that if you have used many other functions in your program,

MATLAB will generate a corresponding HPP and CPP files for each function, you must copy your work.

Cheng Dynasty)

The second case:

1. Built a M file:

%%%%%%%%%%%%%%%%%%%%%

Function fork_2 ()

X = [1 2 3];

Y = [1 3 1];

Plot (x, y);

%%%%%%%%%%%%%%%%%%%%%

After saving, in the command window:

Enter: (format: MCC -B SGLCPP file name)

MCC -B SGLCPP fork_2

2.

3.

4.

The next step is similar to the precedent, just this will come out for a fork_2_mainhg.cpp, fork_2.exe and

bin folder. When copy_2_mainhg.cpp and fork_2.exe when copying. But need

The bin folder is taken into the VC's engineering directory.

5. Add the following header file to the file where the button response function is located:

......

#include "matlab.hpp"

#include "fork_2.hpp"

......

Function Response Code is:

LibmmfileInitialize ();

Libmwsglminitialize ();

MLFHGInitialize (null, null);

Fork_2 ();

MLFHGWAITFORFIGURESTODIE ();

MLFHGTERMINATE ();

Libmmfiletermic ();

Libmwsglmterminate ();

6. Compile, connect, execute, everything OK! (Note that if you have used many other functions in your program,

MATLAB will generate a corresponding HPP and CPP files for each function, you must copy your work.

Cheng Dynasty. In addition, the three lines of code before and before, but it should be essential, but it should be guaranteed.

During the entire implementation of your program, they are only called once, so they will be the best.

Place the program's initialization function that is called by the program that is called by the program.

Appendix A: Link Library

Libmmfile.lib libmatlb.lib libmx.lib libmat.lib libmatpm.lib

SGL.LIB Libmwsglm.lib libmwservices.lib

Appendix B: ignore the library

MSVCRT.LIB

Appendix C: Preprocessing Program Definition

MSVC, IBMPC, MSWind

Appendix D: Further Reference

See the MATLAB Help file for the use of the MCC command

See Matlab online help information about MWARRAY:

http://www.mathworks.com/access/helpdesk_r12p1/help/

Toolbox / mathlib / cppmathug / cppmathug.shtml

"M file transformation into C / C language file" is very simple, but it will encounter a lot of problems through compilation connections in VC.

Even if compiling and connectivity is not guaranteed correctly, sometimes there will be a lot of tricky troubles, in my other article (in the station): "Analyze: Solve the compilation of M-proceration into CPP procedures" Give up

One such example. In that article, although I finally rely on a few tricks, "Xuan Den Road", I got that problem, but

It is still possible to see that this conversion problem is very troublesome.

──────────────────────────────

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

New Post(0)