[Excellent] MATLAB hybrid programming-Part 1: called by other programming languages

Source: Internet
Author: User


Use of MATLAB Compiler (compiler) or MCC-- Personally think it is the right way

1. Set the Compiler:

Http://www.cnblogs.com/fence/archive/2010/10/16/1853276.html

First, you need to configure your own MATLAB compiler. The function of MATLAB compiler is to compile the program into a program that can be directly executed by the machine. To configure compiler, enter the following in the MATLAB command window:

Mbuild-Setup, Follow the prompts to select the built-in MATLAB compiler.

Lab record:

 

Code

> Mbuild-Setup
Please choose your compiler for building standalone MATLAB applications:

Wocould you like mbuild to locate installed compilers [y]/n? Y

Select a compiler:
[1] Lcc-win32 C 2.4.1 In D: \ progra ~ 1 \ MATLAB \ r2007a \ sys \ KP
[2] Microsoft Visual C ++ 6.0 in c: \ Program Files \ Microsoft Visual Studio

[0] None

Compiler: 1

Please verify your choices:

Compiler: Lcc-win32 C 2.4.1
Location: D: \ progra ~ 1 \ MATLAB \ r2007a \ sys \ KP

Are these correct? ([Y]/n): Y

Trying to update options file: C: \ Documents and Settings \ Administrator \ Application Data \ Mathworks \ MATLAB \ r2007a \ compopts. bat
From template: D: \ progra ~ 1 \ MATLAB \ r2007a \ bin \ Win32 \ mbuildopts \ lcccompp. bat

Done...

--> "D: \ progra ~ 1 \ MATLAB \ r2007a \ bin \ Win32 \ mwregsvr D: \ progra ~ 1 \ MATLAB \ r2007a \ bin \ Win32 \ mwcomutil. dll"

Dllregisterserver in D: \ progra ~ 1 \ MATLAB \ r2007a \ bin \ Win32 \ mwcomutil. dll succeeded

--> "D: \ progra ~ 1 \ MATLAB \ r2007a \ bin \ Win32 \ mwregsvr D: \ progra ~ 1 \ MATLAB \ r2007a \ bin \ Win32 \ mwcommgr. dll"

Dllregisterserver in D: \ progra ~ 1 \ MATLAB \ r2007a \ bin \ Win32 \ mwcommgr. dll succeeded

 

Note: because Microsoft Visual C ++ 6.0 is originally installed on my machine, there will be two compilers that will prompt you to choose one. Here, select the one that comes with the system, that is, "1 ".

 

2. Compile the M file that has been written.

Note: mywavelet. M is a test file written by me. The content is as follows:

Function mywavelet
Clear all;
A = [100: 900];
B = SQRT ();
Plot (A, B );

Compile the file in the MATLAB environment:

> MCC-M mywavelet. m

 

 

Mywavelet is the M file to be converted into exe. After the execution is completed, some new files will appear in the current directory of MATLAB. Observe pai_^. These files are compiled files. Click the EXE file to check whether the image is normal?

Copy them to a new folder and keep them safe ~


3. Search for the required dynamic link library

MCR is the runtime environment of MATLAB. For EXE files generated using different MATLAB versions that consume less than MB, The MCR version is also different. Therefore, when the program is packaged, it is best to pack the MCR of the corresponding version together.

Find the mcrinstaller.exe file on the machine where the machine has been installed. My file is in this directory:
D: \ Program Files \ MATLAB \ r2007a \ toolbox \ compiler \ deploy \ Win32 \ mcrinstaller.exe

Different MATLAB versions may have different paths. Find them by yourself. (Mglinstaller.exe ).

After finding it, copy it to the folder we keep, so that there are two types of things in our file home: 1. MCR installer, 2. compiled program.

Dong Guo of "deep dive into php7.x hybrid programming:

"

Release of compiled programs (focusing on dynamic link library files:

Whether compiled into executable files or dynamic link libraries, MCR must be installed on the target computer to publish them to other computers without Matlab installation. You can use the MATLAB command buildmcr to create the MCR installation file that can be released to the end user. Buildmcr is used to create a zip file that contains all the files required by MCR. Buildmcr can be called in either of the following ways:

> Zipfile = buildmcr

Zipfile =
D: \ tools \ MATLAB \ toolbox \ compiler \ deploy \ Win32 \ mcrinstaller.zip

You can also use the input dirnameto specify the mcrinstaller.zip storage directory.

> Zipfile = buildmcr (dirname)

> Buildmcr ('.')

Save the downloaded mcrintaller.zip file to the current working directory.


Generally, there are three files to be published:

(1) compiled executable files or library functions;

(2) CTF file; (3) mcrintaller.zip

On the ultimate user's computer, first unzip the mcrintaller.zip file and add the executable files in the MCR directory to the system path. Then, you can use the compiled program.

Note: MCR does not support adding search paths using the addpath command in startup. M.

Startup. M is a personalized setting that allows users to set some MATLAB operations. If you encounter an error while running the published program. The addpath command must be valid only when it is not released. Use the isdeployed command:

 

If ~ Isdeployed
Addpath (...);
% Other programs
End

 

 

In addition, you need to decompress the CTF when running the compiled program. Therefore, when running, make sure that you have write power to the directory where the CTF file is located. Otherwise, an error is reported when the released program is executed. If the compiled program is released as a CD, the program must be copied to a directory on the hard disk before running.

"


4. Transplant the folder to another computer.

It is best not to install MATLAB on this computer to check whether executable programs can be executed in environments without Matlab. Generally, there is a problem, because there are still a lot of dynamic link libraries that have not been tested, and many items cannot be found during the program running.

To install mcrinstaller.exe, double-click the installation directory and select our current directory. Make sure that the program we compiled is in the same directory as the file after MCR is installed.

Click the executable file to display the image. Try it.


The method to run the executable program in MATLAB is to add one in front !, For example :! Picshow. The suffix is optional.
Before running the EXE file on other machines without Matlab installation:
First install the runtime environment of Matlab. Different Versions of MATLAB environments can coexist on the same machine (in other words, different versions are not compatible ).
The next step is to add the “mcrinstaller.exe installation directory \ Runtime \ Win32 to the environment variable of the computer, which is usually automatically loaded.
If you do not have one, you can also install it manually by adding the following methods:
Right-click "my computer", "property", "advanced", "environment variable", and "add" to specify a variable name, and copy the above path to it.
Note: Microsoft. netframework installation is not required during installation.
Finally, copy the relevant files generated by compilation to the same directory and double-click them to run the program.
Problem: Currently, this method can run completely on computers without Matlab or C/C ++ installed, but it can run on amd cpu, however, there will be no MATLAB compiling interface.


The disadvantage is that the black window of DOS always exists during operation. The following describes how to remove the black screen:

How to eliminate dos black screen of exe program generated by running MATLAB
After the EXE file is generated based on MATLAB, the DOS black screen problem exists every time it is run. You can solve the problem through the following methods:

Method 1: Enter
CD (prefdir)
Edit compopts. bat
Add the following statement at the end of the opened file:

A. VC environment:

Set linkflags = % linkflags %/subsystem: Windows/entry: maincrtstartup

B. The following is an example of the following in the local environment:
Set linkflags = % linkflags %-subsystemwindows

C. BORLAND:
Set linkflags = % linkflags %-aa

After saving the file, re-compile the M file and run the generated EXE file without the DOS window.

Method 2: Use the suppress tool:
Convert to the EXE file name you generated. Put suppress.exe (with an icon for turning off the disk and the monitor), and put the modified suppress. ini in the same directory of the generated exe. Execute suppress.exe or the self-generated exe. Of course, you can change the name of suppress.exe to your desired name.

The method 1 generates an EXE after it is used and runs on a machine without any installation of Matlab.
When the two missing files must exist in the same directory as supress.exe, they must be in the same directory.

Implementation of Mixed Programming Based on vs2008 and Matlab r2010b: http://blog.sina.com.cn/s/blog_49cb42490100nm5j.html


For example, for the. exe file to pass parameters: http://www.matlabsky.com/thread-541-1-1.html

> Mycmd = ['newnhello ', num2str (5)]; System (mycmd );

Use mideva to compile a dynamic link library out of the MATLAB environment

Http://vision-yan.spaces.live.com/blog/cns! 2c7c1ea88cc91a69! 253. Entry (Delphi and Matlab data interface solution discussion)
I. Basic Ideas

If you have read the above solutions, you will find that they all depend on the MATLAB environment. How can we completely break away from the MATLAB environment? This is what I have been studying recently, but it is not satisfactory. Although I found the relevant information, it is too far from the actual situation. I wanted to publish this article after all the work was done. Later I thought it would be better for everyone to study it together. Maybe a person can thoroughly understand the mysteries of this article. This method uses a software called mideva and is a MATLAB compilation and development software platform launched by mathtools. (Note: according to the latest situation, mathtools has been acquired by mathwork ), mediva has the compilation and conversion function. It can convert MATLAB functions into compiled MATLAB programs into C ++ DLL, and then call the dynamic library function in Delphi without relying on the malab environment, the premise is that there must be two dynamic link libraries: mdv4300 and ago4300.

Ii. Compilation steps

The steps for implementing mixed programming of Delphi and M files using the mideva platform are as follows:
1. Compile m Files
2. Execute m Files in mideva
3. Generate a dynamic link library (DLL)
4. Call the DLL function in Delphi

Iii. Summary
If it can be implemented, it will save a lot of unnecessary trouble, but the author's problem is that the DLL compiled by mideva cannot be recognized by Delphi-_-B, the two dynamic link libraries, mdv4300 and ag4300, cannot be found at all. If the man-in-law is able to succeed, please let us know. Thank you very much.

Visual c ++ calls matrix <lib>-- Mixed Programming of VC and matrix <lib>

MATCOM is the c ++ compiler for the mideva kernel, which can generate C-MEX and independent applications. MATCOM contains a group of C ++ libraries called Matrix <lib>. It is a matrix mathematical library developed by mathtools and provides a double-precision matrix type-mm, it can be a complex matrix, a real number matrix, a sparse matrix, or even a matrix of N.

In the Visual C ++ project, you can directly program according to the syntax of matrix <lib> to achieve efficient programming.

Install MATLAB add-in Visual C ++

MATLAB add-in is a new tool introduced in Versions later than Matlab 6.0. It can easily generate independent applications and embed the obtained code into other applications. However, the latest versions of MATLAB 7.0 and later do not support add-in.

Call of COM component generated by MATLAB

MATLAB com Builder

ActiveX Control

C/C ++ calls the MATLAB computing Engine

Matlab Engine can use almost all functions of Matlab, but MATLAB software needs to be installed on machines, and the execution efficiency is low. Therefore, this method is less practical and should not be used in commercial application software development.



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.