Calling Matlab in a C program

Source: Internet
Author: User

C program calls Matlab

Method One: Call the MATLAB engine in the C program (equivalent to open a lite version of MATLAB and then enter the command, that is, client/server mode, c program for the client, MATLAB as the local server)

Method Two: The M file is packaged into a DLL file and then called in the C locale

Pros and Cons Analysis:

Method one, easy to implement, can monitor the operation of the program in real time, but the independence is poor, the speed is slow, need to install the full version of MATLAB, and each call will start the matlab.exe process;

Method Two, the implementation of complex, debugging trouble, but only need to install MCR (MATLAB component runtime), cost less resources, there is easy to publish, in the computer without the installation of MATLAB, with the least resources to run MATLAB program.

Demo (method one)

1. VS Configuration (Vs2008 for example)

Select Tools-----> Options-----> VC + + Directories, as shown in Add path. Note Select the corresponding platform (x64 platform for example)

2. MATLAB configuration

Enter the following command on the MATLAB command line and follow the prompts to select the compiler

Mex-setup

Mbuild-setup

3. Add the path system environment variable (restart the machine according to your MATLAB installation path)

4. Create a new project and add the following code (pay attention to selecting the project's Active solution platform)

Matlab.cpp: Defines the entry point of the console application.

//

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include "engine.h"

#pragma comment (lib, "Libeng.lib")

#pragma comment (lib, "Libmx.lib")

#pragma comment (lib, "Libmat.lib")

int main (int argc, char* argv[])

{

//

Engine *ep;

if (! ( EP = Engopen (NULL))) {

fprintf (stderr, "\ncan ' t start MATLAB engine\n");

return exit_failure;

}

int nsample = 50;

Const double PI = 3.1415926;

Double *t = new Double[nsample];

for (int i = 0; i < nsample; i++)

{

T[i] = i * 2 * pi/nsample;

}

Mxarray *t = null, *result = NULL;

T = Mxcreatedoublematrix (1, Nsample, mxreal);

memcpy (void *) MXGETPR (t), (void *) T, nsample*sizeof (t[0]));

Engputvariable (EP, "T", t);

Engevalstring (EP, "Y=sin (T);");

Engevalstring (EP, "Plot (t,y);");

Engevalstring (EP, "title (' Y=sin (t) ');");

Engevalstring (EP, "Xlabel (' t ');");

Engevalstring (EP, "Ylabel (' y ');");

printf ("Hit Return to Continue\n\n");

Fgetc (stdin);

Mxdestroyarray (T);

Engevalstring (EP, "Close;");

Engclose (EP);

return exit_success;

printf ("Hello, world\n");

}

5. Compile, run, the results are as follows:

The MATLAB command line is started first and the drawing is completed.

Detailed functions and data type reference: "C language and MATLAB interface-programming and examples"

Calling Matlab in a C program

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.