4. Data-type mxarray operations
In the above Matlab Engine functions, all data types related to variables are mxarray. Mxarray and a large number of functions starting with Mx are widely used in Matlab Engine programs and matlab c math libraries. Mxarray is a complex data structure, which corresponds to the array in MATLAB. We only need to be familiar with the array type of MATLAB and several common mxarray functions.
In VC, all data interactions with Matlab are implemented through mxarray. In a program using mxarray, the header file matrix should be included. h. However, in engine programs, the header file engine is generally included. h. The file already contains matrix. h, so you do not need to include it again.
4.1 create and clear mxarray data
MATLAB has many types of variables. For each type, there is basically a function for creation, but they all have the same data structure, that is, mxarray.
The establishment of an array is a function in the form of mxcreatexxx. For example, to create a double type array, you can use the mxcreatedoublematrix function. The function form is as follows:
Mxarray * mxcreatedoublematrix (int m, int N, mxcomplexity complexflag );
The M and N parameters are matrix functions and columns. Complexflag is a constant used to distinguish whether the elements in the matrix are real numbers or complex numbers. The values are mxreal and mxcomplex respectively.
For example, create
3
Line
5
Two-dimensional real array of Columns
, The following statement is available:
Mxarray * t = mxcreatedoublematrix (3, 5, mxreal );
To delete an array mxdestroyarray, the function declaration is as follows:
Void mxdestroyarray (mxarray * array_ptr );
The array_ptr parameter is the array pointer to be deleted.
For example, to delete the created array T, you can use the following statement:
Mxdestroyarray (t );
Similar functions include:
Mxarray * mxcreatestring (const char * Str );
Create a string type and initialize it as a STR string.
Generally, in the interaction between VC and MATLAB, the above two types are enough. The creation of other types of arrays is not described here.
4.2 manage mxarray Data Types
4.2.1 manage mxarray data size
To obtain the number of elements in each dimension of the mxarray array, you can use the mxgetm and mxgetn functions. Mxgetm is used to obtain the number of elements in the first dimension of the array. For a matrix, it is the number of rows.
Int mxgetm (const mxarray * array_ptr );//
Return
Array_ptr
Number of elements corresponding to the first dimension of the array (number of rows)
Int mxgetn (const mxarray * array_ptr );//
Return
Array_ptr
Number of elements corresponding to other dimensions of the array.
It is the number of columns. Multi-dimensional arrays are the product of the number of elements in each dimension from the 2nd dimension to the last dimension.
To obtain the number of elements in a specific dimension, use the function:
Const int * mxgetdimensions (const mxarray * array_ptr );
This function returns the number of elements in each dimension of array_ptr stored in an int array. For Commonly Used matrices, you can use mxgetm and mxgetn functions.
In addition, mxgetnumberofdimensions can be used to obtain the total dimension of the array. mxsetm and mxsetn can be used to set the number of rows and columns of the matrix. The functions are described as follows:
Int mxgetnumberofdimensions (const mxarray * array_ptr); // returns the dimension of the array.
Void mxsetm (mxarray * array_ptr, int m); // you can specify M as an array.
Void mxsetn (mxarray * array_ptr, int N); // you can specify N as the array.
4.2.2 determine the mxarray type
Before you operate on mxarray variables, you can verify the data type of the arrays in the following table, for example, whether they are double arrays, integers, strings, and logical values, and whether it is a certain structure, class, or special type, such as whether it is an empty array, whether it is INF, Nan, etc. Common judgment functions include:
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 );
...... ......
These functions are simple, self-explanatory, and will not be explained.
4.2.3 manage mxarray data
For Commonly Used arrays of the double type, you can use the mxgetpr and mxgetpi functions to obtain the data pointers of the actual and virtual parts respectively. The declaration of these two functions is as follows:
Double * mxgetpr (const mxarray * array_ptr); // returns the Real-Time Pointer of the array array_ptr.
Double * mxgetpi (const mxarray * array_ptr); // returns the virtual pointer of the array array_ptr.
In this way, the data in the mxarray array can be read and written through the obtained pointer. For example, you can use the enggetvariable function to read an mxarray array from the MATLAB workspace, and then use mxgetpr and mxgetpi to obtain the data pointer and process the data in it, finally, call the engputvariable function to re-write the modified array to the MATLAB workspace. For detailed implementation, see section 5th program instance.