外部インタフェース/API | ![]() ![]() |
MAT-ファイルをFortranで読み込む
この例題プログラムは、matdemo1.f
で作成されたMAT-ファイルに読み込み、その内容を記述するライブラリルーチンの使用法を説明します。
C matdemo2.f C C This is a simple program that illustrates how to call the C MATLAB MAT-file functions from a Fortran program. This C demonstration focuses on reading MAT-files. It reads in C the MAT-file created by matdemo1.f and describes its C contents. C C Copyright (c) 1996-2000 The MathWorks, Inc. C C-------------------------------------------------------------- C $ Revision: 1.7 $ C program matdemo2 C-------------------------------------------------------------- C (integer) Replace integer by integer*8 on the DEC Alpha C platform. C integer matOpen, matGetDir, matGetNextMatrix integer mp, dir, adir(100), pa C-------------------------------------------------------------- C C Other variable declarations here C integer mxGetM, mxGetN, matClose integer ndir, i, stat character*32 names(100), name, mxGetName C C------------------------------------------------------------- C Open file and read directory. C------------------------------------------------------------- C mp = matOpen('matdemo.mat', 'r') if (mp .eq. 0) then write(6,*) 'Can''t open ''matdemo.mat''.' stop end if C C Read directory. C dir = matgetdir(mp, ndir) if (dir .eq. 0) then write(6,*) 'Can''t read directory.' stop endif C C Copy integer into an array of pointers. C call mxCopyPtrToPtrArray(dir, adir, ndir) C C Copy integer to character string C do 20 i=1,ndir call mxCopyPtrToCharacter(adir(i), names(i), 32) 20 continue C write(6,*) 'Directory of Mat-file:' do 30 i=1,ndir write(6,*) names(i) 30 continue C stat = matClose(mp) if (stat .ne. 0) then write(6,*) 'Error closing ''matdemo.mat''.' stop end if C C------------------------------------------------------------- C Reopen file and read full arrays. C------------------------------------------------------------- C mp = matOpen('matdemo.mat', 'r') if (mp .eq. 0) then write(6,*) 'Can''t open ''matdemo.mat''.' stop end if C C Read directory. C write(6,*) 'Getting full array contents:' pa = matGetNextMatrix(mp) do while (pa .ne. 0) C C Copy name to character string. C name = mxGetName(pa) write(6,*) 'Retrieved ', name write(6,*) ' With size ', mxGetM(pa), '-by-', mxGetN(pa) pa = matGetNextMatrix(mp) end do C stat = matClose(mp) if (stat .ne. 0) then write(6,*) 'Error closing ''matdemo.mat''.' stop end if stop C end
このプログラムをコンパイル、リンクした後で、結果を見ることができます。
matdemo2 Directory of Mat-file: String Numeric Getting full array contents: 1 Retrieved String With size 1-by- 33 3 Retrieved Numeric With size 3-by- 3
![]() | MAT-ファイルをFortranで作成 | MAT-ファイルプログラムのコンパイルとリンク | ![]() |