外部インタフェース/API | ![]() ![]() |
文字列配列を渡す
文字列配列を渡すことは、この節の前の例「文字列を渡す」よりもわずかに複雑です。MATLABは行の代わりに列単位として行列を保存するので、文字列配列のサイズがFortran MEX-ファイル内で正しく定義されていることは必須です。重要な点は、MATLABで定義される行と列のサイズが、Fortran MEX-ファイルでは反対にならなければならないことです。従ってMATLABに出力するときは、出力行列は転置されなければなりません。
つぎの例は、文字列配列やキャラクタ行列をワークスペースに直接置くのではなく、出力引数としてMATLABに置きます。MATLAB内で、この関数を呼び出すためにつぎのようにタイプします。
passstr;
これは、5行15列の行列mystring
を得ます。いくつかの操作を行う必要があります。オリジナルの文字列行列は、サイズが5行15列です。MATLABの読み込み方法と行列内の要素の方向のために、行列のサイズはMEXファイルでM=15
とN=5
と定義されなければなりません。行列がMATLAB内に設定された後、行列は転置されなければなりません。
C $ Revision: 1.11 $ C============================================================== C C passstr.f C Example for illustrating how to pass a character matrix C from Fortran to MATLAB. C C Passes a string array/character matrix into MATLAB as C output arguments rather than placing it directly into the C workspace. C C This is a MEX-file for MATLAB. C Copyright (c) 1984-2000 The MathWorks, Inc. C============================================================== 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 p_str, mxCreateString C-------------------------------------------------------------- C integer nlhs, nrhs integer i character*75 thestring character*15 string(5) C Create the string to passed into MATLAB. string(1) = 'MATLAB ' string(2) = 'The Scientific ' string(3) = 'Computing ' string(4) = 'Environment ' string(5) = ' by TMW, Inc.' C Concatenate the set of 5 strings into a long string. thestring = string(1) do 10 i = 2, 6 thestring = thestring(:((i-1)*15)) // string(i) 10 continue C Create the string matrix to be passed into MATLAB. C Set the matrix size to be M=15 and N=5. p_str = mxcreatestring(thestring) call mxSetM(p_str, 15) call mxSetN(p_str, 5) C Transpose the resulting matrix in MATLAB. call mexCallMATLAB(1, plhs, 1, p_str, 'transpose') return end
passstr
ans = MATLAB The Scientific Computing Environment by TMW, Inc.
![]() | 文字列を渡す | 行列を渡す | ![]() |