Mxarray access __linux for matlab/c++ mixed programming

Source: Internet
Author: User

In matlab/c++ mixed programming, all data types related to variables are mxarray types, so access to Mxarray is essential, data structure Mxarray and a large number of MX start functions, widely used in MATLAB and C, C + + mixed programming.

Mxarray's related operations are:

1, create and clear the Mxarray type data  

MATLAB has a number of variable types, corresponding to each type, basically have a function to create, but they all have the same data structure, is mxarray.

The creation of arrays takes the form of mxcreatexxx functions, such as creating an array of double types, which can be mxcreatedoublematrix with functions as follows:

Mxarray *mxcreatedoublematrix (int m, int n, mxcomplexity complexflag);

The parameters m and n are the number of rows and columns of the matrix. Complexflag is a constant that distinguishes between the elements in a matrix as real or complex, and the values are Mxreal and Mxcomplex respectively. For example, to create a two-dimensional real array of 3 rows and 5 columns, you can use the following statement:

Mxarray *t = Mxcreatedoublematrix (3, 5, mxreal);

To delete an array of Mxdestroyarray, the function declares the following:

void Mxdestroyarray (Mxarray *array_ptr);

Parameter array_ptr is the array pointer to delete.

For example, to delete the array t created above, you can use the following statement:

Mxdestroyarray (T);

Similar creation functions include the following:

Mxarray *mxcreatestring (const char *STR);

Creates a string type and initializes it to a str string.

In general VC and MATLAB Interaction, the above two types is enough, other types of array creation here no longer introduced.

2. Manage Mxarray data types

2.1 Manage Mxarray Data size

To get the number of elements on each dimension of the Mxarray array, you can use the Mxgetm and MXGETN functions. Where Mxgetm is used to get the number of elements in the first dimension of the array, which is the number of rows for the matrix.

int Mxgetm (const mxarray *array_ptr); Returns the number of elements (rows) of the first dimension of the array_ptr corresponding array

int Mxgetn (const mxarray *array_ptr); Returns the number of elements in the other dimensions of the array_ptr corresponding array, which is the number of columns for the matrix.

For multidimensional arrays is the product of the number of dimension elements from the 2nd to the last dimension.

To get the number of elements for a particular dimension, use a function:

const int *mxgetdimensions (const mxarray *array_ptr);

The function returns the number of elements in the Array_ptr dimension and is saved in an int array. For commonly used matrices, it is OK to use the Mxgetm and MXGETN two functions.

In addition, we can get the total dimension of the array by Mxgetnumberofdimensions, and set the number of rows and columns of the matrix with Mxsetm, MXSETN, the function description is as follows:

int mxgetnumberofdimensions (const mxarray *array_ptr); Returns the number of dimensions of an array

void Mxsetm (Mxarray *array_ptr, int m); Set Array to M row

void Mxsetn (Mxarray *array_ptr, int n); Set an array to n columns

2.2 Judging Mxarray array types  

Before you operate on a variable of type Mxarray, you can verify the data type of the array below, such as whether it is a double array, an integer, a string, a logical value, etc., and whether it is a struct, class, or special type, such as whether it is an empty array, an INF, Nan, and so on. The common judgment functions are:

BOOL Mxisdouble (const mxarray *array_ptr);

BOOL Mxiscomplex (const mxarray *array_ptr);

BOOL Mxischar (const mxarray *array_ptr);

BOOL Mxisempty (const mxarray *array_ptr);

BOOL Mxisinf (double value);

2.3 Management Mxarray the data for the array

For a commonly used array of double types, you can use the MXGETPR and MXGETPI two functions to obtain the data pointers for both the fact and the imaginary part, which are declared as follows:

Double *MXGETPR (const mxarray *array_ptr); Returns the real part pointer of an array array_ptr

Double *MXGETPI (const mxarray *array_ptr); Returns the imaginary part pointer of an array array_ptr

This allows you to read and write data from an array of mxarray types by obtaining a pointer to it. For example, you can use function enggetvariable to read the Mxarray type array from the MATLAB workspace, then obtain the data pointer with MXGETPR and MXGETPI, and then process the data. Finally, the Engputvariable function is called to write the modified array back to the MATLAB workspace.

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.