| MATLAB Application Program Interface | Search  Help Desk |
| mxCreateDoubleMatrix | Examples See Also |
Create an unpopulated 2-dimensional, double-precision, floating-point mxArray
#include "matrix.h" mxArray *mxCreateDoubleMatrix(int m, int n, mxComplexity ComplexFlag);m
mxREAL or mxCOMPLEX. If the data you plan to put into the mxArray has no imaginary components, specify mxREAL. If the data has some imaginary components, specify mxCOMPLEX.
A pointer to the created mxArray, if successful. If unsuccessful in a stand-alone (nonMEX-file) application, mxCreateDoubleMatrix returns NULL. If unsuccessful in a MEX-file, the MEX-file terminates and control returns to the MATLAB prompt. mxCreateDoubleMatrix is unsuccessful when there is not enough free heap space to create the mxArray.
Use mxCreateDoubleMatrix to create an m-by-n mxArray. mxCreateDoubleMatrix initializes each element in the pr array to 0. If you set ComplexFlag to mxCOMPLEX, mxCreateDoubleMatrix also initializes each element in the pi array to 0.
If you set ComplexFlag to mxREAL, mxCreateDoubleMatrix allocates enough memory to hold m-by-n real elements. If you set ComplexFlag to mxCOMPLEX, mxCreateDoubleMatrix allocates enough memory to hold m-by-n real elements and m-by-n imaginary elements.
Call mxDestroyArray when you finish using the mxArray. mxDestroyArray deallocates the mxArray and its associated real and complex elements.
Create a 2-by-4 double-precision, floating-point mxArray, then populate it:
int rows=2, cols=4;
double pr_data[] = {5.2, 7.9, 1.3, 4.2, 6.7, 5.9, 1.9, 9.4};
double pi_data[] = {3.4, 6.5, 2.2, 9.1, 8.3, 4.7, 2.5, 7.5};
double *start_of_pr, *start_of_pi;
mxArray *array_ptr;
/* Create a 2-by-4 real double matrix named "B". */
array_ptr = mxCreateDoubleMatrix(rows, cols, mxCOMPLEX);
mxSetName(array_ptr, "B");
/* Populate the real part of the created array. */
start_of_pr = (double *)mxGetPr(array_ptr);
memcpy(start_of_pr, pr_data, rows * cols * sizeof(double) );
/* Populate the imaginary part of the created array. */
start_of_pi = (double *)mxGetPi(array_ptr);
memcpy(start_of_pi, pi_data, rows * cols * sizeof(double) );
For an additional example, see mxcreatedoublematrix.c in the mx subdirectory of the examples directory.
mxCreateNumericArray, mxComplexity