External Interfaces |
Reading a MAT-File in Fortran
This sample program illustrates how to use the library routines to read in the MAT-file created by matdemo1.f
and describe its contents.
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) 1984-2000 The MathWorks, Inc. C $Revision: 1.11 $ program matdemo2 integer matOpen, matClose, matGetDir integer matGetNextVariable, matGetNextVariableInfo integer mxGetM, mxGetN integer mp, dir, adir(100), pa integer ndir, i, stat character*32 names(100), name C Open file and read directory. mp = matOpen('matdemo.mat', 'r') if (mp .eq. 0) then write(6,*) 'Can''t open ''matdemo.mat''.' stop end if dir = matgetdir(mp, ndir) if (dir .eq. 0) then write(6,*) 'Can''t read directory.' stop endif C Copy pointer into an array of pointers. call mxCopyPtrToPtrArray(dir, adir, ndir) C Copy pointer to character string. do 20 i=1,ndir call mxCopyPtrToCharacter(adir(i), names(i), 32) 20 continue write(6,*) 'Directory of Mat-file:' do 30 i=1,ndir write(6,*) names(i) 30 continue stat = matClose(mp) if (stat .ne. 0) then write(6,*) 'Error closing ''matdemo.mat''.' stop end if C Reopen file and read full arrays. mp = matOpen('matdemo.mat', 'r') if (mp .eq. 0) then write(6,*) 'Can''t open ''matdemo.mat''.' stop end if C Get Information on first array in mat file. write(6,*) 'Getting Header info from first array.' pa = matGetVariableInfo(mp, names(1)) write(6,*) 'Retrieved ', names(1) write(6,*) ' With size ', mxGetM(pa), '-by-', mxGetN(pa) call mxDestroyArray(pa) write(6,*) 'Getting Header info from next array.' pa = matGetNextVariableInfo(mp, name) write(6,*) 'Retrieved ', name write(6,*) ' With size ', mxGetM(pa), '-by-', mxGetN(pa) call mxDestroyArray(pa) C Read directory. write(6,*) 'Getting rest of array contents:' pa = matGetNextVariable(mp, name) C Copy name to character string. do while (pa .ne. 0) i=mxGetM(pa) write(*, *) i write(6,*) 'Retrieved ', name write(6,*) ' With size ', mxGetM(pa), '-by-', mxGetN(pa) call mxDestroyArray(pa) pa = matGetNextVariable(mp, name) end do stat = matClose(mp) if (stat .ne. 0) then write(6,*) 'Error closing ''matdemo.mat''.' stop end if stop end
After compiling and linking this program, you can view its results.
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
Creating a MAT-File in Fortran | Compiling and Linking MAT-File Programs |
© 1994-2005 The MathWorks, Inc.