MATLAB Application Program Interface Reference | Help Desk |
mxSetPi
Set new imaginary data for anmxArray
#include "matrix.h" void mxSetPi(mxArray *array_ptr, double *pi);array_ptr
Pointer to a full (nonsparse) mxArray
.
Pointer to the first element of an array. Each element in the array contains the imaginary component of a value. The array must be in dynamic memory; call mxCalloc
to allocate this dynamic memory. If pi
points to static memory, memory leaks and other memory errors may result.
mxSetPi
to set the imaginary data of the specified mxArray
.
Most mxCreate
functions optionally allocate heap space to hold imaginary data. If you tell an mxCreate
function to allocate heap space (for example, by setting the ComplexFlag
to mxComplex
or by setting pi
to a non-NULL
value), then you do not ordinarily use mxSetPi
to initialize the created mxArray's
imaginary elements. Rather, you typically call mxSetPi
to replace the initial imaginary values with new ones.
Create a 1-by-5 mxArray
. Then, grow the mxArray
to 1-by-6, seeding it with the five elements of the 1-by-5 mxArray
.
mxArray *array_ptr; double start_pr[5] = {3.2, 4.6, 5.1, 6.8, 7.3}; double start_pi[5] = {4.5, 4.2, 4.4, 4.8, 4.6}; double *new_heap_pr, *new_heap_pi, *pr, *pi; /* Create a 1-by-5 complex array of doubles. */ array_ptr = mxCreateDoubleMatrix(1, 5, mxCOMPLEX); pr = mxGetPr(array_ptr); pi = mxGetPi(array_ptr); memcpy((void *)pr,(const void *)start_pr,5*sizeof(double)); memcpy((void *)pi,(const void *)start_pi,5*sizeof(double)); mxSetName(array_ptr, "Apricots"); ... /* Add a sixth element to Apricots. */ /* First, allocate heap to hold six complex elements. */ new_heap_pr = (double *)mxCalloc(6, sizeof(double)); new_heap_pi = (double *)mxCalloc(6, sizeof(double)); /* Next, copy the old five complex elements to a new section. */ memcpy(new_heap_pr, start_pr, 5*sizeof(double)); memcpy(new_heap_pi, start_pi, 5*sizeof(double)); /* Next, assign the 6th complex element. */ new_heap_pr[5] = 8.9; new_heap_pi[5] = 4.7; /* Next, free the heap required to hold the old pr and pi. */ mxFree(mxGetPr(array_ptr)); mxFree(mxGetPi(array_ptr)); /* Now, change Apricot's dimensions to 1-by-6. */ mxSetN(array_ptr, 6); /* Finally, associate the new numbers with Apricots. */ mxSetPr(array_ptr, new_heap_pr); mxSetPi(array_ptr, new_heap_pi);For an additional example, see
mxSetPi.c
in the mx
subdirectory of the examples
directory.
mxGetPi, mxGetPr, mxSetPr