I don't know whether Matlab actually crashes or not, but letting me圎rrMsgTxt() print some useful, intelligible information and gracefully abort the function and return to the Octave prompt is much less unsettling. I have found that if mexFunction() is fed stuff that it doesn't like, Octave is likely to crash. Nrout = 62 nrout = 65 nrout = 68 nrout = 71 Nrout = 50 nrout = 53 nrout = 56 nrout = 59 Nrout = 38 nrout = 41 nrout = 44 nrout = 47 Nrout = 26 nrout = 29 nrout = 32 nrout = 35 Nrout = 14 nrout = 17 nrout = 20 nrout = 23 * For debugging purposes: Print out the input array */ * equivalent and copy to output array region * Now Convert the resulting array back to a column-major * Work on nrin with some C++ function */ Output_pointer = (Doub *)mxGetData(plhs) * Get addresses of start of data regions of octave arguments * This will be same shape/size as the input argument. * Allocate the space for the return argument. Mat3DDoub nrout(dimension_array, dimension_array, dimension_array) Mat3DDoub nrin(dimension_array, dimension_array, dimension_array) * The C++ arrays are the same shape/size as the Fortran arrays. * Declare and allocate storage for C++ 3D input and output arrays. * The dimension_array pointer gives the size of each dimensionĭimension_array = mxGetDimensions(prhs) Number_of_dimensions = mxGetNumberOfDimensions(prhs) Me圎rrMsgTxt("Input array must be of type double.") Me圎rrMsgTxt("Only one output argument is allowed.") Me圎rrMsgTxt("One input argument is required.") * Check for proper number of input and output arguments. Void mexFunction(Int nlhs, mxArray * plhs, Int nrhs, const mxArray * prhs) * This function always has this signature */ Void Foo(Mat3DDoub_I & x, Mat3DDoub & y) Void C_to_Fortran(Doub * dest, Mat3DDoub & src) * Copy C++ 3D array to column-major array contents */ Void Fortran_to_C(Mat3DDoub & dest, Doub * src) * Copy colum-major array contents to C++ 3D array */ * This includes just about all of the usual C++ headers, among other things */ * As far as I know, it should work for Matlab, but. * test3d.cpp illustrates use of GNU Octave 3D arrays used Here's the cpp function source of a simple test program: I mean, the compiler won't complain if you ignore my warnings (it's not illegal in C++ to do such things), but I'm betting that the default copy constructor and assignment operator really, really (really) won't do what you have in mind. The Mat3DDoub class, as supplied in the Numerical Recipes code, does not have a copy constructor or an overloaded assignment operator, so you can't just set one equal to another, and functions with Mat3DDoub arguments should always call by reference. (So, if you try something like memcpy() to copy the arrays, you will have to "transpose" from one storage scheme to the other, even though the Mat3DDoub data values are stored in contiguous memory.)ģ. Mat3DDoub objects have data that is stored C++-style (row-major order) with pointers to pointers set up so the data can be accessed with C++-style indexed notation. That is, elements are stored in contiguous memory locations in column-major order.Ģ. Arrays in Matlab and Octave are Fortran-style arrays. As far as I know, it "should" work with Matlab, but I have no way of testing.ġ. I can give an example that works with GNU Octave.