C language and MATLAB interface programming and Example Li Shunjun

Source: Internet
Author: User
Tags scalar

A list of your previous study of C language and Matlab mixed notes, by the way review. "C language and MATLAB interface programming and examples Li Shunjun" (not finished, now see P106)

Catalogue P4-8

C-mex function: A function written in C or Fortran from MATLAB.  mex file: A dynamic link function that is automatically called and executed by a MATLAB interpreter in a format that is written in C or Fortran language. In Windows systems, the type suffix of the Mex file is named Dll,win7 in the 64-bit computer as MEXW64 (MEXSG in Linux). The  matlab system stipulates that the execution priority of MEX files is higher than M files. The  mex file is used in the same way as the MATLAB built-in function. Because the Mex file cannot display the corresponding function information, in general, when constructing a Mex file, a matlab m file is written as its help interpretation file and stored in the same directory. The M file cannot contain any executable statements and contains only some help information.   There are three files of the same name in the same directory, corresponding to ". C" files, ". dll" files, ". M" files. Mxarray the interface between C language and MATLAB is a mxarray structure written by C language. For all data types in MATLAB, such as numeric arrays (double precision, single precision, int8, Int16, UInt16, Int32, UInt32, etc.), strings, discrete matrices, cell arrays, structs, objects, multidimensional arrays, logical arrays, empty arrays. These MATLAB data types can be defined using the Mxarray structure. Library functions in the external interface function Library of MATLAB, there are 4 kinds of library functions related to C. C Engine Routines, C mat-file Routines, C mex-functions and C MX Functions, respectively. Among them, C Mex Functions and C MX Functions are respectively the MEX and MX prefixes, respectively, to complete different functions. The MX function library provides a way for users to create, access, manipulate, and delete Mxarray structure objects in the C language. The main function of the MEX function library is to interact with the MATLAB environment, obtain the necessary array data from the MATLAB environment, and return certain information, including text hint, data array, etc.。 The method of writing, compiling, and linking C-mex functions using the MEX directive is not only able to create a C-language Mex function, You can also create a FORTRAN-language MEX function. P16 Table 2.1 Mex Command Control Parameters Mex [Option1...optionn] sourcefile1 [... sourcefilen] [Objectfile1...objectfilen] [Libraryfile1 ... libraryfilen];  where option is the command-line parameter option for the MEX directive; sourcefile all C original files for the Mex file to participate in the compilation; ObjectFile is the object file that participates in the compilation ; Libraryfile is the library file that participates in the compilation.   Writing source code: the use of code writers, such as VC + +, MATLAB, such as writing C language source code. Compile: Compile the source code to generate the target file. Link: The source file generated by the target file, the necessary library files and other target files are linked together to generate the final executable program files. Mex-setup Command >>mex-setup; % Install compiler  >>edit; %  Write C-mex function file >>edit  **.c% write C-mex function file ****************************************************** The difference between C and MATLAB storage: In C, the data is stored in rows, and if read by columns, an exception occurs, and in MATLAB, when the data is stored by columns, an exception occurs if read by rows. The C-mex function is a MATLAB interface function written in C language. When writing C-mex functions, be sure to follow the data storage and reading methods in the Matlab language. The header file is #include "mex.h" interface program function is: void Mexfunction (      int NLHS,       &NBsp;                    //number of Left (output) parameters      mxarray *plhs[],               //array of left parameters      int NRHS,                            //Right (input) number of parameters      const Mxarray *prhs[]      An array of the right arguments) {...}   Write the basic flow of C-mex function: 1, write C-mex interface function. 2. Define the type and dimension of input and output variables. 3, check the number of input and output parameters. 4, check the input parameters of the data type. 5. Get the dimension of input parameter. 6. Check the dimension condition of input parameters. 7, set the dimension of the output parameter. 8, generate the output parameters of the Mxarray structure. 9, get the input parameters of the pointer. 10, get the pointer of the output parameter. 11, call C subroutine. 12, write the C calculation subroutine, and put it before the mexfunction function. 13, compile the link. 14. Analysis and testing. When a variable is used to represent a scalar, the value of the variable must be obtained from the Mxarray struct using the Mxgetscalar () function. When a pointer variable is used to represent a scalar, the value of the variable must be obtained from the Mxarray struct using the MXGETPR () function. Mxisdouble: Determine if the array isThe double-precision type.                bool mxIsDouble (const Mxarray *array_ptr); Mxiscomplex: Determines whether the array is a complex type.                bool MxIsComplex ( Const Mxarray *array_ptr); Mxgetm: Gets the number of rows of the array.                int MxGetM (const Mxarray * array_ptr); MXGETN: Gets the number of columns in the array.                int MxGetN (const Mxarray * array_ptr); MXGETPR: Gets the data pointer of the real part of the array.                double *mxGetPr (const Mxarray *array_ptr) MXGETPI: Gets the data pointer of the imaginary part of the array.                double *mxGetPi (const Mxarray *array_ptr) Mxgetscalar: Gets the first data of the real part of an array.              &Nbsp; double mxgetscalar (const mxarray *array_ptr); Mxcreatedoublematrix: Creates a two-dimensional array of unassigned double-precision floating-point types.                mxarray * Mxcreatedoublematrix (int m,int n,mxcomplexity complexflag); Mxcreatescalardouble: Creates a scalar array of unassigned double-precision floating-point types.                mxarray * Mxcreatescalardouble (double value); ****************************************************** Multidimensional array common functions: mxgetnumberofdimensions: Gets the number of dimensions of the array.                int Mxgetnumberofdimensions (const mxarray *array_ptr); Mxgetdimensions: Gets an array pointer to the size of the array dimension.                const int * Mxgetdimensions (const mxarray *array_ptr); Mxcreatenumbericarray: Creates an unassigned numeric array of n-dimensions.                mxarray* MxcreatenumeriCArray (int ndim,const int *dims,mxclassid class,mxcomplexity complexflag) mxgetnumberofelements: Gets the number of elements in the array.                int Mxgetnumberofelements (const mxarray *array_ptr); ****************************************************** String function: Mxischar: Determines whether the array is a string-type array.                bool MxIsChar (const Mxarray *array_ptr); mxgetstring: Gets the contents of the string array.                int mxGetString (const Mxarray*array_ptr,char*buf,int Buflen) Mxcreatestring: Creates a string array of 1*n dimensions.                mxarray * mxcreatestring (const char *str); ******************************************************p86 The corresponding relational table function between data types describes: Mxissingle: Determines whether the data type of the array is a single-precision floating-point type.                boolMxissingle (const mxarray *array_ptr); MxIsInt8: Determines whether the data type of the array is a 8-bit integer type.                bool MxIsInt8 (const Mxarray *array_ptr) MxIsInt16: Determines whether the data type of the array is a 16-bit integer type.                bool mxIsInt16 (const Mxarray *array_ptr) MxIsInt32: Determines whether the data type of the array is a 32-bit integer type.                bool MxIsInt32 (const Mxarray *array_ptr) MxIsUint8: Determines whether the data type of the array is a 8-bit integer type.                bool MxIsUint8 (const Mxarray *array_ptr) MxIsUint16: Determines whether the data type of the array is a 16-bit integer type.                bool mxIsUint16 (const Mxarray *array_ptr) MxIsUint32: Determines whether the data type of the array is a 32-bit integer type.                bool MxIsUint32 (const Mxarray *arRAY_PTR); Mxgetdata: Gets the pointer to the array (the return value is void).                void *mxGetData (const Mxarray *array_ptr) Mxgetimagedata: Gets a pointer to the array's imaginary part data (the return value is void).                void *mxGetImagData ( Const Mxarray *ARRAY_PTR) Mxcreatenumericmatrix: Creates a two-dimensional unassigned numeric array.                mxarray * Mxcreatenumericmatrix (int m,int n,mxclassid class,mxcomplexity complexflag); Mxcreatenumericarray: Creates an unassigned numeric array of n-dimensions.                mxarray * Mxcreatenumericarray (int ndim,const int *dims,mxclassid class,mxcomplexity complexflag); Mxsetdata: Sets the data pointer for the array.                void MxSetData ( Mxarray *array_ptr,void *data_ptr); Mxsetimagdata: Sets the virtual part data pointer for the array.      &nbSp;         void mxsetimagdata (Mxarray *array_ptr,void *pi); ******** c Call the MATLAB function Mexcallmatlab: Used to invoke MATLAB built-in functions or user-defined MATLAB files and Mex files.                int MexCallMATLAB (int Nlhs,mxarray*plhs[],int nrhs,mxarray*prhs[],const char *command_name) Mxdestroyarray: Releases the memory occupied by the MxArray array.                void MxDestroyArray ( Mxarray *array_ptr); ****************************************************** Structures Array Function Description: Mxisstruct: Determines whether the array is a struct array.                bool mxIsStruct (const mxarray* array_ptr); Mxgetnumberofdimensions: Gets the number of dimensions of the array.                int Mxgetnumberofdimensions (const mxarray *array_ptr); Mxgetdimensions: Getting an array that points to the size of the array dimension refers toNeedle.                const int * Mxgetdimensions (const mxarray*array_ptr); Mxgetnumberoffields: The number of domains that get the structure array.                int Mxgetnumberoffields (const mxarray *array_ptr); Mxgetfieldnamebynumber: Gets the domain name by index.                const char* Mxgetfieldnamebynumber (const mxarray *array_ptr,int field_number); Mxgetfieldnumber: Gets the index value of the specified domain name.                int MxGetFieldNumber ( Const Mxarray *array_ptr,const char*field_name); Mxgetfield: Gets the value of the specified field for the specified element of the struct array.                mxarray*mxgetfield ( Const Mxarray*array_ptr,int index,const char*field_name); Mxcalcsinglesubscript: Gets the index value of the specified element.            &Nbsp;   int mxcalcsinglesubscript (const mxarray*array_ptr,int nsubs,int *subs); Mxcreatestructarray: Creates an n-dimensional unassigned structure array.                mxarray* Mxcreatestructarray (int ndim,const int *dims,int nfields,const char**field_names); ********************************* *********************

C language and MATLAB interface programming and instance Li Shunjun is compiled with

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.