The method of realizing the M file in MATLAB 6.5 by C language

Source: Internet
Author: User
Tags command line mathematical functions

As we all know, Matlab is a powerful mathematical software, good at using matrix operations to complete a variety of mathematical functions. But the program needs to be interpreted and implemented in the MATLAB environment, the efficiency is not high. If you can use its powerful library for C language, use C to compile and execute, MATLAB will be able to play a greater role. So, Matlab has already provided the application interface with the external C + + program from 5.0, which provides the possibility for using the function of the C language to invoke Matlab. But the interface of MATLAB development is very fast, to MATLAB 6.5 already provided to VC 7.0 support, at the same time to C interface relative to 5.X version has a certain change.

In the MATLAB, we use the M file to implement functions, each of the M file to achieve a separate function, this point and C language functions are mutually corresponding. So, if we can transform the M file in MATLAB into a single function under C language, we can realize the corresponding function in MATLAB.

Implementation methods

The whole process can be divided into three main parts, using MATLAB to translate m file into C language file, extract useful statements from the generated C language file, and write data conversion program to realize parameter format conversion. The whole process will eventually translate the M file into a function of the same function in C language for other programs to call.

This article uses one of the simplest m files to sample:

FileName: messay.m

function c=messay()
a=3.4;
b=5.6;
c=sqrt(a)+sqrt(b);

The M file implements the calculation

1, the M file compiled into C language files

In order to translate m files into C language files, it is necessary to set up a certain setting, where the C-language environment is written as VC6.0, at the MATLAB command prompt, enter mex-setup and Mbuild-setup commands, select Microsoft Visual C + + in the corresponding options + + can be.

In the MATLAB command line, the MCC command is used to translate MESSAY.M into C code.

mcc -m messay.m

The parameter-m represents the MCC command, which translates the M file into C language code.

The translation command will generate three C language files in the same folder as MESSAY.M: Messay.h,messay.c and MESSAY_MAIN.C. The MESSAY_MAIN.C provides the main () function, Messay.h provides the function declaration of the whole program, and Messay.c contains the function function of MATLAB. Of these three files, messay.c contains the mathematical functions we need.

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.