C + + invoke MATLAB program

Source: Internet
Author: User
A hybrid programming method of Matlab and C + +

Development environment: Win7 64-bit +vs2010+matlab

If you want to let Matlab call the C + + function class, you need to compile C + + into MEX files, this method can be used to achieve mixed debugging. But I think that learning C + + and MATLAB mixed programming is commonly called MATLAB function through C + +, because the MATLAB has a powerful mathematical function library, however, VC + + has the advantage of flexible interface design, so here only introduced through the VC + + call Matlab method.

(1) The Environment configuration constructs

MATLAB configuration: Before the beginning of the MATLAB need to install two compiler plug-ins, this installation directly in the MATLAB command window in the input command:

>>mex-setup

>>mbuild–setup






Either enter directly during the installation or select the relevant compiler.

VC + + project configuration: After installation, configure C + + program contains directories, library directory This corresponds to your computer MATLAB installation location of the. h and Lib file point to. \extern\include 、.. \extern\lib\win64\microsoft.




In addition, add the libraries Libmx.lib, Libmat.lib, libeng.lib that are required to invoke Eigin in linker-attached dependencies, and the library Mwarray the classes needed to invoke Mclmcrrt.lib,mwarray are connected to C + + and the need for Matlab matrix class, if you do not understand the use of this class need to look at the online usage. Also in the system of bad variables, add matlab dynamic link library. dll file location, this is located in the Matlab installation bin directory.

(2) Program call

Then you'll start learning how to call Matab's library of functions through C + +, which has two methods:

The first method: in C + + directly invoke the MATLAB computing engine eigin.

To add a Eigin header file to the called file:

#include <engine.h>

Then call the Eigin function where the program wants to refer to MATLAB:

Engine *ep;
Ep=engopen (NULL);
Engevalstring (EP, "X=0:0.05:2*pi;y=sin (x);p lot (X,y, '. B ')");


I am here to call Matlab through C + + to draw a positive function as an example, in the Engevalstring function is the input command function code, of course, this function can be in the input of the string of only one command, and then call the Engevalstring function multiple times, You can also directly take all the MATLAB function code directly as a string, input engevalstring function. Test the result of the call:


I in the example through the MFC dialog box Open button, opened to read the picture, and call the MATLAB function of the drawing function, you can see at the same time MFC and MATLAB to draw a window of coexistence. I like to use MATLAB and C + + mixed programming reason is this, C + + logical debugging is more difficult, but if the MATLAB display C + + program running some data changes in the results of debugging, it is quite cool.

This method of calling Eigin is a disadvantage that requires running the program of the computer installed with Matlab, this method I do not like, because in the company to work, often to their own demo results to others to test, and other people's computer does not install MATLAB, This will not invoke the MATLAB eigin

The second method: using the MATLAB function as a dynamic link library, and then call the dynamic link library in C + +: here to test the input data through C + + to Matlab, which is a picture of one of the channel data, Then in the MATLAB display the single channel picture, finally returns the data to the C + +. Using this method, the Mwarray class is used to realize data transmission.

MATLAB picture data display function[C + +]View plain copy function [Output_args] = Test (Input_args) [n m]=size (Input_args);   A=uint8 (Input_args ');   Imshow (A);   output_args=[3,4,5]; End then in the MATLAB command window input The following command: >>mcc-w cpplib:plotsintest-t link:lib TEST.M will generate the name: Plotsintest dynamic link library, static link library, header file, and finally VS, properties linker additional Dependencies Plotsintest.lib
Mclmcrrt.lib
Libmx.lib
Libmat.lib
Libeng.lib

Then add the header file Plotsintest.h in C + +, and then call the code where the program runs:[C + +]  View Plain Copy if ( ! mclinitializeapplication (null,0)  )    {                fprintf (stderr,  "could not  initialize the application.\n ");       exit (1);  }       if  (! plotsintestinitialize ()  )    {        fprintf (stderr, "could not initialize the library.\n");        exit (1);  }   int width=m_porignimage->getwidth ();   int heigth =m_porignimage->getheight ();   double *r=new double[heigth*width];   for  ( int i=0;i


The final result is:

You can see that the MFC dialog box reads the display picture, while Matlab appears to draw the figure window. Original address: http://blog.csdn.net/hjimce/article/details/44904271 Author: hjimce

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.