MATLAB calls C language

Source: Internet
Author: User

First, be familiar with the C language API included with MATLAB
1, define the mexfunction function, the definition of mexfunction is unique: it can only be the following form:
void mexfunction (int nlhs, Mxarray *plhs[],int nrhs, const Mxarray *prhs[])
There is no change in the name and parameter type, and you can call the C + + program you just defined in the Mexfunciton function.

2, Matfile matopen (const char *filename, const char mode)--open/create

3, Matfile matopen (const char *filename, const char mode)--opens/creates a mat file;

4, int matclose (Matfile *pmf)--Close a mat file;

5, Mxarray *mxcreatedoublematrix (int m, int n, mxcomplexity flag)--Create a (complex) double-precision matrix;

6, ' Mxarray *mxcreatesparse (int m, int n, int nzmax,mxcomplexity flag)--Create a sparse matrix;

7. Mxarray *matgetnextarray (Matfile *pmf)--Get the next matrix inside the mat file;

8, const char *mxgetname (const Mxarray *PA)--obtains the name of the Matrix PA;

9, void Mxsetname (Mxarray *pa,const char *s)--Set a name for the matrix PA;

10, int Mxgetm (const mxarray *PA)--obtains the total number of the Matrix PA;

11, int mxgetn (const mxarray *PA)--Obtain the total number of columns of the Matrix PA;

12, double *MXGETPR (const Mxarray *PA)--Obtain the PR pointer of the matrix PA;

13, int *mxgetir (const mxarray *PA)--obtains the IR pointer of sparse matrix PA;

14, int *MXGETJC (const mxarray *PA)--obtains the JC pointer of sparse matrix PA;

15, int Matputarray (Matfile * PMF, const mxarray * PA)--store The matrix PA into Mat file pmaf;

16, void Mxdestroyarray (Mxarray *pa)-Release The Matrix PA (remove it from memory);

Second, example (using C + + analog matlab Matrix addition add.cpp)

#include   mex.h   #include <stdlib.h> #include  <string.h>void mexfunction (int  nlhs, mxarray *plhs[], int nrhs, const mxarray *prhs[]) {double * INDATA1,&NBSP;*INDATA2,&NBSP;*OUTDATA;INT&NBSP;INM1,&NBSP;INN1,&NBSP;INM2,&NBSP;INN2,&NBSP;OUTM,&NBSP;OUTN,  i, j;//Check the number of input parameter matrices if  (nrhs != 2) mexerrmsgtxt ("Input parameter must be 2!") ");else if  (nlhs > 2) mexerrmsgtxt (" too many output arguments ");// Gets the row and column of the input matrix Inm1 = mxgetm (Prhs[0]); INN1&NBSP;=&NBSP;MXGETN (Prhs[0]); Inm2 = mxgetm (prhs[1]); INN2&NBSP;=&NBSP;MXGETN (Prhs[1]);outm =  (inm1 > inm2)  ? inm1 : inm2 ;outn =  (INN1&NBSP;&GT;&NBSP;INN2)  ? inN1 : inN2;      mexprintf ("Output lines:%d\n", Outm)       mexprintf ("Output columns:%d\n", outn);//Create output matrix Plhs[0]  = mxcreatedoublematrix (outm, outn, mXreal);//Get input, output the head pointer INDATA1&NBSP;=&NBSP;MXGETPR (prhs[0]); INDATA2&NBSP;=&NBSP;MXGETPR (prhs[1]); outdata = &NBSP;MXGETPR (Plhs[0]);//Create a two-dimensional array double **temp1, **temp2,**answer1,**answer2;     temp1 =  (double **) malloc (sizeof (double *)  * inm1);for  (i = 0;  i < inm1; i++) temp1[i] =  (double *) malloc (sizeof (double)  *  inN1);temp2 =  (double **) malloc (sizeof (double *)  * inm2);for  (i =  0; i < inm2; i++) temp2[i] =  (double *) malloc (sizeof (double)  * &NBSP;INN2);        answer1 =  (double **) malloc (sizeof ( double *) &NBSP;*&NBSP;OUTM);for  (i = 0; i < outm; i++) answer1[i]  =  (double *) malloc (sizeof (double)  * outn);        answer2 =  (Double **) malloc (sizeof (double *)  * outm);for  (I&NBSP;=&NBSP;0;&NBSP;I&NBSP;&LT;&NBSP;OUTM ;  i++) answer2[i] =  (double *) malloc (sizeof (double)  * outn);     //Standard Answer1 Initial assignment is 0    for  ( i = 0; i < outM;  i++) for  (int j = 0; j < outn; j++) answer1[i][j]=0;    for  ( i = 0; i < outm; i++) for  (int j =  0; j < outn; j++) answer2[i][j]=0;    //temp One-dimensional variable two-dimensional  for   ( i = 0; i < inm1; i++) for  (int j = 0; j  < inn1; j++) TEMP1[I][J]&NBSP;=&NBSP;INDATA1[J*INM1&NBSP;+&NBSP;I];//TEMP2 One-dimensional transformation two-dimensional       for  (i = 0; i < inm2; i++)    for  (  j = 0; j < inn2; j++) temp2[i][j] = indata2[j*inm2 +i];      &NBSP;&NBSP;//TEMP1 Two-dimensional extension to standard       {      for  (  i = 0; i < inm1; i++)     for  ( j =  0; j <inn1; j++)  answer1[i][j] = temp1[i][j];} &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//TEMP2 Two-dimensional extension to standard       {       for  ( i = 0; i < inm2; i++)     for   ( j = 0; j <inn2; j++)  answer2[i][j] = temp2[i][j];} The standard format is added and the result is entered into the answer1     for  (i = 0; i < outm;  i++) {    for  (j = 0; j < outn; j++)   ANSWER1[I][J]=&NBSP;ANSWER1[I][J]+&NBSP;ANSWER2[I][J];}       mexprintf ("\ n");      //releases the fractional group temp1for  ( i  = 0; i < inm1; i++) {free (temp1[i]);} Free (TEMP1);//Release decimal Group temp2for  ( i = 0; i < inm2; i++) (Temp2[i]);} Free (TEMP2);               // Release Large Array answer2for  ( i = 0; i < outm; i++) {free (answer2[i]);} Free (answer2);//two-dimensional turn one-dimensional output!!  for  (i = 0; i < outm; i++)     for  (j  = 0; j < outn; j++)     {outdata[j*outm+i] =answer1[ i][j];    } //  release Large Array answer1for  ( i = 0; i <  outm; i++) {free (answer1[i]);} Free (answer1);}


Third, compile
Copy the ADD.C to the current directory in MATLAB, execute the MEX add.cpp, and generate the ADD.MEXW64, which implements the SUM function. This function can now be called in MATLAB


This article from "Big Tao Eat Dumplings" blog, declined reprint!

MATLAB calls C language

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.