"Go" C + + calls Matlab's. m file

Source: Internet
Author: User

Original Addressmatlab is a powerful mathematical computing/simulation tool with a lot of useful out-of-the-box functions built in, and we often define many m functions ourselves. But in many cases, we have to use VC programming. So, how to use the resources of MATLAB in VC? Here I briefly illustrate with a simple example if the. m file defined in MATLAB is called in the VC. A wide range of theories will not be said, a concise example. I believe everyone will learn it immediately after they have seen it.part of the gray bottom display is the part that needs to be entered manually. The steps are as follows:㈠ Of course is the first to establish MATLAB m file. for the sake of simplicity here, I have defined a function in MATLAB whose m file is as follows: function y = Add_zh (A, b) y=a+b Save As MYFUNCT.M㈡ The following to set the MATLAB compilation environment.

Open Matlab, and in the command line state, type:mex-setup

then the following prompt appears:--------------------------------------------------------------------------------------Select a compiler:[1] Borland C++builder version 5.0 in C:\Program Files\borland[2] Lcc C version 2.4 in F:\SOFT\SHARE\MATLAB65\SYS\LCC[3] Microsoft Visual C + + version 6.0 in C:\Program Files\Microsoft Visual Studio[0] None Compiler:--------------------------------------------------------------------------------------Here we choose ' 3 '. (If you use a different compiler such as BCB, then you can select ' 1 ' accordingly)then, continue typing in the command line state:Mbuild–setup--------------------------------------------------------------------------------------Choose your compiler for building standalone MATLAB applications:would mbuild to locate installed compilers [y]/n? ySelect a compiler:[1] Borland C++builder version 5.0 in C:\Program Files\borland[2] Lcc C version 2.4 in F:\SOFT\SHARE\MATLAB65\SYS\LCC[3] Microsoft Visual C + + version 6.0 in C:\Program Files\Microsoft Visual Studio[0] None Compiler:--------------------------------------------------------------------------------------here are a few steps to confirm. You can operate according to the situation. ㈢ Set the compiler environment for the VC. in Tools-options-directory, set the path of include and LIB, add one item, point to \extern\include and \extern\lib in MATLAB installation directory ㈣ compiling. m files into a dynamic-link library
This step is more critical. In this step, convert the . m file in Matlab to a dll. in the MATLAB command line, enter:mcc-t-h-l c-w lib:ppp-t link:libmyfunct.m at this point, you can see that a lot of files have been generated in the directory where MYFUNCT.M is located. We only use 3 of them:ppp.h ppp.lib Ppp.dll ㈤ testing and calling
1. Create a new VC project. The name is Mat. For simplicity, we create a dialog here to demonstrate. As follows:
Then, the above 3 files are copied to the VC project in the same directory, so that the VC call.  2. Do VC programming. first, in the CPP file, add: #include "ppp.h"then, set the VC's link library. In the Object/librarymodules under Project-settings-link, add:ppp.liblibmx.lib libmatlb.lib libmmfile.libat this point, the VC compilation environment is set up. Here is the programming section. 3. key to the programming part: conversion of interfaces and data types first give the VC implementation of the source code:
void Cmatdlg::onbutton1 () {//m file defined in MATLAB: MYFUNCT, its function is to complete two number of add operations.            CString str;            UpdateData (TRUE);            Pppinitialize ();            In order to invoke a function in MATLAB, you must use the array data type, and then call the MATLAB function to convert it to a matrix format (the basic data type of MATLAB is a matrix) static double x1[1]={1.0};            static Double x2[1]={2.5};            Double result;            Call Matlab to create a 3 matrix Mxarray *a=mclgetuninitializedarray ();            Mxarray *b=mclgetuninitializedarray ();            Mxarray *c=mclgetuninitializedarray ();            Assigns the variable value in C language to the Matrix Mlfassign (&a,mlfdoublematrix (1,1,x1,null) in MATLAB);            Mlfassign (&b,mlfdoublematrix (1,1,x2,null));            Mlfassign (&c,mlfmyfunct (A, b));            Pass a pointer to a matrix in MATLAB to the pointer to double double * MD=MXGETPR (c) in the C language;            RESULT=MD[0];            Release these matrices Mxdestroyarray (A);            Mxdestroyarray (B);            Mxdestroyarray (C); Str.            Format ("It is:%f", result); :: AfxMessageBox (str);} 

believe in the comments inside, we will understand the almost. VC Programming to achieve the use of matlab function calls, such as VC We implement two double type of the addition, You need to do the programming above: first define two arrays and store the data we want to function (that is, the so-called arguments), then convert the two arrays into a matrix that Matlab can recognize, and then invoke the custom function in the M file. To manipulate the matrix, and then revert the result back to the data format supported by the VC: double * MD=MXGETPR (C);Result=md[0]; at this point, the data processed by the MATLAB function has been deposited into the VC 's result variable.         Mlfassign (&c,mlfmyfunct (A, b));As you can see, when making a call, we use the file name myfunct of M files instead of the Add_zh (A, b) defined in the M file . in the program, we want to implement The addition of 1.0 and 2.5, the result is as follows: .....

Run Error: E:\Program files\matlab\r2009a\extern\include\mclmcr.h (1764): Error C2065: ' REFCLSID ': undeclared identifier
E:\Program Files\matlab\r2009a\extern\include\mclmcr.h (1764): Error c2146:syntax error:missing ') ' before identifier ' Clsid
E:\Program Files\matlab\r2009a\extern\include\mclmcr.h (1764): Error C2182: ' mclclsidtochar_proxy ': illegal use of type ' void '
E:\Program Files\matlab\r2009a\extern\include\mclmcr.h (1764): Error c2059:syntax Error: ') '
E:\Program Files\matlab\r2009a\extern\include\mclmcr.h (1768): Error C2065: ' Refguid ': undeclared identifier
E:\Program Files\matlab\r2009a\extern\include\mclmcr.h (1768): Error c2146:syntax error:missing ') ' before identifier ' Guid
Not resolved

"Go" C + + calls Matlab's. m file

Related Article

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.