External Interfaces Previous page   Next Page

Passing Matrices

In MATLAB, you can pass matrices into and out of MEX-files written in Fortran. You can manipulate the MATLAB arrays by using mxGetPr and mxGetPi to assign pointers to the real and imaginary parts of the data stored in the MATLAB arrays. You can create new MATLAB arrays from within your MEX-file by using mxCreateDoubleMatrix.

This example takes a real 2-by-3 matrix and squares each element.

After performing error checking to ensure that the correct number of inputs and outputs was assigned to the gateway subroutine and to verify the input was in fact a numeric matrix, matsq.f creates a matrix for the argument returned from the computational subroutine. The input matrix data is then copied to a Fortran matrix by using mxCopyPtrToReal8. Now the computational subroutine can be called, and the return argument can then be placed into y_pr, the pointer to the output, using mxCopyReal8ToPtr.

For a 2-by-3 real matrix

typing

produces this result


Previous page  Passing Arrays of Strings Passing Two or More Inputs or Outputs Next page

© 1994-2005 The MathWorks, Inc.