A summary of the mixed programming between MATLAB and C + +

Source: Internet
Author: User
Tags visual studio 2010

"Reprint Please specify source" Http://www.cnblogs.com/mashiqi

First, summarize:

Because of the features of the function input and output parameters of C/C + + language, multiple parameters can be conveniently passed into a function, but it is not convenient to return multiple parameters. To return multiple arguments, you have to use pointers or struct/class (other tricks?). But the trouble is that there is not much problem between the function and function of C program, and the key is not to complicate the interface between C and MATLAB.

However, when we write MATLAB code, because the format of the variable is very free, so we often try to format the variable as a very comprehensible form. For example, I wrote the M function Adaboost_mashiqi (), which accomplishes the function of the adaboost algorithm, which has both a vector, a matrix, and a struct in its return value. This appears to be complex in C, but it is intuitive, and these variables should be in this form. Even if it is a multi-storey tree, it should also be linked list it!

The contents of these two paragraphs can be expressed in the following picture.

Explain this picture. The leftmost is an M file, which is ready to invoke a function named True_function () written in C + +. We simply record the input and output of this function as inputs and outputs, so the function should be this:

Since inputs and outputs are all variables in MATLAB, they are likely to be comprehensible. After Mexfunction (), the first thing to do is to convert these comprehensible but not convenient variables into a C-language easy-to-handle convenient variable, and then invoke the True_ that actually implements the function. function () functions to implement the function, and finally, these convenient but not comprehensible variables into the appropriate comprehensible with MATLAB variables.

However, this is not the best thing to do, because the variables in mexfunction () that deal with these comprehensible but not convenient can be quite cumbersome. converting variable types is not the advantage of C/C + +, and the advantage of C + + is that the processing of loops is much faster than matlab! so we have a second solution that avoids the disadvantages of C + +, while preserving the advantages of C + +, which is a two-way solution. Please see:

This scheme puts the work of parameter conversion into MATLAB. This is an absolutely correct method, because the type conversion between the variables in MATLAB is quite free. Let's take a look at how the whole process turns out after this change. First, the main file calls the interface () function, the parameters are passed in MATLAB, it is very convenient to enter the interface () function, on the MATLAB platform on the various variables into the C + + platform good processing format ( For example, each member variable of the structure is passed in separately instead of the entire structure as a whole, etc); now to Mexfunction (), because the variable in interface () has been processed, You just need to do some more trivial processing here, and then call True_function () directly to implement the specific function. The whole process is very easy!.

Of course, there are some problems, such as in MATLAB, we often put a number of variables control the entire algorithm parameters into a structure variable options, and then the structure of the incoming function control (such as the maximum number of iterations of the algorithm, the convergence condition of the epsilon size ah, etc.). How can this options be implemented in the framework above? I'm here to give a way for me. First, call options = Optionssettings (...) before interface (). function, set the corresponding control variable, and then pass the options struct variable along with inputs to the interface () function. Within the interface () function, the individual member variables of the options structure are taken out as variables into the true_function (), so it is possible.

Bo Master to a example Ah!

Above said seems to be good abstract, here to a link, is one of my code, we can take to see, as example, in my this above change, practice practiced hand. But I have some flaws in this code. Since the entire code has been written, back to comb the time only to find these problems, so lazy to change, the next attention is just. Here is the question of my code, write it here, make a record of yourself.

The processing of the variable tree in this code about me

In the struct returned by Adaboost_mashiqi (), the first member variable tree itself is also a struct, but as stated above, the criterion for passing parameters between C functions is as comprehensible as possible, So there's no problem with the tree approach here. The problem arises in the second place where the tree appears, that is, the tree that returns to the M file, where the tree is still a struct, which is not very good, the first is that the variable format is not very convenient to convert, but if you want to add function add function to return more variables, it is not convenient to use the structure in this case. Therefore, the M file and the C file in the middle of a M file to complete the "variable format conversion" This thing, so that it can not break the format of the original M file, but also make the C file structure easier to write and extend.

How do I hybrid programming between MATLAB and the Visual Studio platform?

Not much nonsense to say, want to know how to set up Visual Studio to do mixed programming debugging, click here. In addition, I use the version is Matlab r2012b and Visual Studio 2010, these two are matched, if not the use of these two versions, here to see if your two versions match.

about the self-accomplishment of the Writing program (/ cover face):

Absolutely not!

Optimization is a must to optimize;

Can perfect it must be perfect;

Can write more beautiful must be able to write more beautiful!

A summary of the mixed programming between MATLAB and C + +

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.