C Math Library Reference    
mlfAssign

Assign an array value to a variable

C Prototype

Arguments
mxArray **dest
   The address of a pointer to the target array (the left-hand side of an assignment statement). You must initialize *dest to NULL or to a valid array.

mxArray *src
   A pointer to the value you want to assign (the right-hand side of an assignment statement)

Return

Returns *dest, the pointer to the target array.

Description

By default, all the arrays returned by the MATLAB C Math Library routines are temporary arrays. This allows you to nest calls to library routines as arguments to other routines. You do not need to deallocate the arrays returned by the nested calls; the library routines delete them automatically. (Arrays that are returned by routines that you write, using mlfReturnValue(), are also temporary.)

To make an array persist, you must bind the array to a variable using the mlfAssign() routine. This routine replaces the standard C assignment operator (=). You must explicitly free arrays that are bound to variables.

mlfAssign() assigns src to *dest. src points to the source array. *dest is equivalent to the left-hand side of an assignment statement. src is equivalent to the right-and side of an assignment statement.

If *dest already points to a valid array, mlfAssign() destroys that array before assigning the source array to it. However, if *dest points to an input argument to the current function, different rules apply. If *dest points to a temporary array, it is destroyed; if it points to a bound array, the assignment does not take place.

Functions that take output arguments (mxArray** arguments), including the indexed assignment functions, follow the same rules as mlfAssign() when deleting existing valid arrays.

If src, the right-hand side of the assignment, points to a bound array, *dest, receives a copy of the array. The copy is a shared-data copy. The actual data associated with the array is not copied until a function modifies the data in the array, for example, a call to mlfIndexAssign() modifies two rows of an array. At that point the data itself is copied to the array before the modifications are made, and the array is no longer points to shared data.

For example,

modifies the value at position (2,2) in array B. At that point, the library copies the data to itself before the modifications are made, and the array no longer points to shared data.

Shared data itself is not freed until all arrays that use the data have been destroyed. The functions mxGetPr() and mxGetPi() that access data stored in an array directly handle shared data correctly; mxSetPr() and mxSetPi() modify the data correctly.

Example

This code assigns the matrix product of array Q and array R to *Z

where Q, R, and Z are mxArray* variables. Z is initialized to NULL and Q and R point to existing arrays.

If you decide not to use the automated memory management features of the library, this code performs the same matrix multiplication.

See Also

mlfIndexAssign


 mlfArrayRef mlfColon