External Interfaces/API |
 |
Dynamically Allocating Memory
It is possible to allocate memory dynamically in a Fortran MEX-file, but you must use %val
to do it. This example takes an input matrix of real data and doubles each of its elements.
C $ Revision: 1.12 $
C===============================================================
C
C dblmat.f
C Example for illustrating how to use %val.
C Doubles the input matrix. The demo only handles real part
C of input.
C NOTE: If your Fortran compiler does not support %val,
C use mxCopy_routine.
C
C NOTE: The subroutine compute() is in the file called
C compute.f.
C
C This is a MEX-file for MATLAB.
C Copyright (c) 1984-2000 The MathWorks, Inc.
C
C===============================================================
C Gateway subroutine
subroutine mexfunction(nlhs, plhs, nrhs, prhs)
C---------------------------------------------------------------
C (pointer) Replace integer by integer*8 on the DEC Alpha
C platform.
C
integer plhs(*), prhs(*)
integer pr_in, pr_out
integer mxGetPr, mxCreateFull
C---------------------------------------------------------------
C
integer nlhs, nrhs, mxGetM, mxGetN
integer m_in, n_in, size
if(nrhs .ne. 1) then
call mexErrMsgTxt('One input required.')
endif
if(nlhs .gt. 1) then
call mexErrMsgTxt('Cannot return more than one output.')
endif
m_in = mxGetM(prhs(1))
n_in = mxGetN(prhs(1))
size = m_in * n_in
pr_in = mxGetPr(prhs(1))
C mxCreateFull dynamically allocates memory.
plhs(1) = mxCreateFull(m_in, n_in, 0)
pr_out = mxGetPr(plhs(1))
C Call the computational routine.
call compute(%val(pr_out), %val(pr_in), size)
return
end
C $ Revision: 1.3 $
C===============================================================
C
C compute.f
C
C This subroutine doubles the input matrix. Your version of
C compute() may do whaveter you would like it to do.
C
C This is a MEX-file for MATLAB.
C Copyright (c) 1984-2000 The MathWorks, Inc.
C
C===============================================================
C Computational subroutine
subroutine compute(out_mat, in_mat, size)
integer size, i
real*8 out_mat(*), in_mat(*)
do 10 i=1,size
out_mat(i) = 2*in_mat(i)
10 continue
return
end
For an input 2-by-3 matrix
x = [1 2 3; 4 5 6];
typing
y = dblmat(x)
yields
y =
2 4 6
8 10 12
Note
The dblmat.f example, as well as fulltosparse.f and sincall.f , are split into two parts, the gateway and the computational subroutine, because of restrictions in some compilers.
|
| Handling Complex Data | | Handling Sparse Matrices |  |