| 外部インタフェース/API | ![]() |
Fortran MEX-ファイルからの関数の呼び出し
API関数mexCallMATLABを使って、Fortranソースコード内からMATLAB関数、演算子、M-ファイル、他のMEX-ファイルを呼び出すことができます。つぎの例は、mxArrayを作成し、サブ関数に様々なポインタを渡してデータを取得し、mexCallMATLABを呼び出して正弦関数を計算し、結果をプロットします。
C $ Revision: 1.8 $
C
================================================================
C
C sincall.f
C
C Example for illustrating how to use mexCallMATLAB.
C
C Creates an mxArray and passes its associated pointers (in
C this demo, only pointer to its real part, pointer to number
C of rows, pointer to number of columns) to subfunction fill()
C to get data filled up, then calls mexCallMATLAB to calculate
C sin function and plot the result.
C
C NOTE: The subfunction fill() is in the file called fill.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)
integer nlhs, nrhs
C---------------------------------------------------------------
C (pointer) Replace integer by integer*8 on the DEC Alpha
C platform.
C
integer plhs(*), prhs(*)
integer rhs(1), lhs(1)
integer mxGetPr, mxCreateFull
C---------------------------------------------------------------
C
integer m, n, max
C initializition
m=1
n=1
max=1000
rhs(1) = mxCreateFull(max, 1, 0)
C Pass the pointer and variable and let fill() fill up data.
call fill(%val(mxGetPr(rhs(1))), m, n, max)
call mxSetM(rhs(1), m)
call mxSetN(rhs(1), n)
call mexCallMATLAB(1, lhs, 1, rhs, 'sin')
call mexCallMATLAB(0, NULL, 1, lhs, 'plot')
C Clean up the unfreed memory after calling mexCallMATLAB.
call mxFreeMatrix(rhs(1))
call mxFreeMatrix(lhs(1))
return
end
C $ Revision: 1.3 $
C
================================================================
C
C fill.f
C This is the subfunction called by sincall that fills the
C mxArray with data. Your version of fill can load your data
C however you would like.
C
C This is a MEX-file for MATLAB.
C Copyright (c) 1984-2000 The MathWorks, Inc.
C
C
================================================================
C Subroutine for filling up data.
subroutine fill(pr, m, n, max)
real*8 pr(*)
integer i, m, n, max
m=max/2
n=1
do 10 i=1,m
10 pr(i)=i*(4*3.1415926/max)
return
end
Fortran計算サブルーチン内からmexCallMATLAB(または他のAPIルーチン)を使うことができます。倍精度データをもつほとんどのMATLAB関数のみ呼び出しが可能であることに注意してください。eigのような計算を実行するM-ファンクションは、倍精度でないデータに対しては正常に動作しません。
sincall
つぎの例は、2つの変数を出力し、それらのうちの1つだけに値を割り当てるM-ファイルを作成します。 function [a,b]=foo[c] a=2*c;
MATLABは、以下のワーニングメッセージを表示します。
Warning: One or more output arguments not assigned during call to 'foo'.mexCallMATLABを使ってfooを呼び出す場合、割り当てられていない出力変数は、タイプmxUNKNOWN_CLASSです。
| スパース行列の操作 | アドバンスドトピックス | ![]() |