MATLAB Application Program Interface | Search  Help Desk |
mxSetPr | Examples See Also |
Set new real data for an mxArray
#include "matrix.h" void mxSetPr(mxArray *array_ptr, double *pr);array_ptr
mxArray
.
pr
mxCalloc
to allocate this dynamic memory. If pr
points to static memory, then memory leaks and other memory errors may result.
Use mxSetPr
to set the real data of the specified mxArray
.
All mxCreate
calls allocate heap space to hold real data. Therefore, you do not ordinarily use mxSetPr
to initialize the real elements of a freshly-created mxArray
. Rather, you typically call mxSetPr
to replace the initial real values with new ones.
Create a populated 2-by-3 real-only mxArray
. Then, change its data.
mxArray *array_ptr; double start_pr[6] = {3.2, 4.6, 5.1, 6.8, 7.3, 8.1}; double *start_of_real_data, *next_data, *pr; int c; /* Create a 2-by-3 real-only array of doubles. */ array_ptr = mxCreateDoubleMatrix(2, 3, mxREAL); pr = mxGetPr(array_ptr); memcpy((void *)pr,(const void *)start_pr,6*sizeof(double)); mxSetName(array_ptr, "Apricot"); ... /* Redo "Apricot", changing its size and its real data. */ /* First, deallocate the old pr data. */ mxFree(mxGetPr(array_ptr)); /* Next, change Apricot's dimensions to 2-by-4. */ mxSetN(array_ptr, 4); /* Next, allocate memory to hold 8 real elements. */ start_of_real_data = (double *)mxCalloc(8, mxGetElementSize(array_ptr)); /* Place 8 values in the newly allocated memory. */ next_data = start_of_real_data; for (c=12; c<20; c++) *next_data++ = sqrt(c); /* Associate the new data with the real data of Apricots. */ mxSetPr(array_ptr, start_of_real_data);
Apricot
initially contains
3.2000 5.1000 7.3000 4.6000 6.8000 8.1000After calling
mxSetPr
, Apricot
contains
3.4641 3.7417 4.0000 3.6056 3.8730 4.1231For an additional example, see
mxsetpr.c
in the mx
subdirectory of the examples
directory.
mxGetPr
, mxGetPi
, mxSetPi