User`s manual

Troubleshooting
1-39
Solution
Call mxDestroyArray instead.
mxDestroyArray(temp); /* CORRECT */
Incorrectly Constructing a Cell or Structure mxArray
You cannot call mxSetCell or mxSetField variants with prhs[] as the member
array.
Warning
Warning: You are attempting to use an array from another scope
(most likely an input argument) as a member of a cell array or
structure. You need to make a copy of the array first. MATLAB will
attempt to fix the problem and continue, but this will result in
memory faults in future releases.
Example That Causes Warning
In the following example, when the MEX-file returns, MATLAB will destroy
the entire cell array. Since this includes the members of the cell, this will
implicitly destroy the MEX-file’s input arguments. This can cause several
strange results, generally having to do with the corruption of the caller’s
workspace, if the right-hand side argument used is a temporary array (i.e., a
literal or the result of an expression).
myfunction('hello')
/* myfunction is the name of your MEX-file and your code */
/* contains the following: */
mxArray *temp = mxCreateCellMatrix(1,1);
...
mxSetCell(temp, 0, prhs[0]); /* INCORRECT */
Solution
Make a copy of the right-hand side argument with mxDuplicateArray and use
that copy as the argument to
mxSetCell (or mxSetField variants); for example
mxSetCell(temp, 0, mxDuplicateArray(prhs[0])); /* CORRECT */