External Interfaces |
Creating a MAT-File in Fortran
This example creates a MAT-file, matdemo.mat
.
C matdemo1.f 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 writing MAT-files. C C matdemo1 - Create a new MAT-file from scratch. C C Copyright (c) 1984-2000 The MathWorks, Inc. C $Revision: 1.9 $ program matdemo1 integer matOpen, matClose integer matGetVariable, matPutVariable integer matPutVariableAsGlobal, matDeleteVariable integer mxCreateDoubleMatrix, mxCreateString integer mxIsFromGlobalWS, mxGetPr integer mp, pa1, pa2, pa3, pa0, status double precision dat(9) data dat / 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 / C Open MAT-file for writing. write(6,*) 'Creating MAT-file matdemo.mat ...' mp = matOpen('matdemo.mat', 'w') if (mp .eq. 0) then write(6,*) 'Can''t open ''matdemo.mat'' for writing.' write(6,*) '(Do you have write permission in this directory?)' stop end if C Create variables. pa0 = mxCreateDoubleMatrix(3,3,0) call mxCopyReal8ToPtr(dat, mxGetPr(pa0), 9) pa1 = mxCreateDoubleMatrix(3,3,0) pa2 = mxCreateString('MATLAB: The language of computing') pa3 = mxCreateString('MATLAB: The language of computing') status = matPutVariableAsGlobal(mp, 'NumericGlobal', pa0) if (status .ne. 0) then write(6,*) 'matPutVariableAsGlobal ''Numeric Global'' failed' stop end if status = matPutVariable(mp, 'Numeric', pa1) if (status .ne. 0) then write(6,*) 'matPutVariable ''Numeric'' failed' stop end if status = matPutVariable(mp, 'String', pa2) if (status .ne. 0) then write(6,*) 'matPutVariable ''String'' failed' stop end if status = matPutVariable(mp, 'String2', pa3) if (status .ne. 0) then write(6,*) 'matPutVariable ''String2'' failed' stop end if C Whoops! Forgot to copy the data into the first matrix -- C it is now blank. Well, ok, this was deliberate. This C demonstrates that matPutVariable will overwrite existing C matrices. call mxCopyReal8ToPtr(dat, mxGetPr(pa1), 9) status = matPutVariable(mp, 'Numeric', pa1) if (status .ne. 0) then write(6,*) 'matPutVariable ''Numeric'' failed 2nd time' stop end if C We will delete String2 from the MAT-file. status = matDeleteVariable(mp, 'String2') if (status .ne. 0) then write(6,*) 'matDeleteVariable ''String2'' failed' stop end if C Finally, read back in MAT-file to make sure we know what C we put in it. status = matClose(mp) if (status .ne. 0) then write(6,*) 'Error closing MAT-file' stop end if mp = matOpen('matdemo.mat', 'r') if (mp .eq. 0) then write(6,*) 'Can''t open ''matdemo.mat'' for reading.' stop end if pa0 = matGetVariable(mp, 'NumericGlobal') if (mxIsFromGlobalWS(pa0) .eq. 0) then write(6,*) 'Invalid non-global matrix written to MAT-file' stop end if pa1 = matGetVariable(mp, 'Numeric') if (mxIsNumeric(pa1) .eq. 0) then write(6,*) 'Invalid non-numeric matrix written to MAT-file' stop end if pa2 = matGetVariable(mp, 'String') if (mxIsString(pa2) .eq. 0) then write(6,*) 'Invalid non-string matrix written to MAT-file' stop end if pa3 = matGetVariable(mp, 'String2') if (pa3 .ne. 0) then write(6,*) 'String2 not deleted from MAT-file' stop end if C Clean up memory. call mxDestroyArray(pa0) call mxDestroyArray(pa1) call mxDestroyArray(pa2) call mxDestroyArray(pa3) status = matClose(mp) if (status .ne. 0) then write(6,*) 'Error closing MAT-file' stop end if write(6,*) 'Done creating MAT-file' stop end
Once you have compiled and linked your MAT-file program, you can run the stand-alone application you have just produced. This program creates a MAT-file, matdemo.mat
, that can be loaded into MATLAB. To run the application, depending on your platform, either double-click on its icon or enter matdemo1
at the system prompt.
To verify that the MAT-file has been created, at the MATLAB prompt enter
whos -file matdemo.mat Name Size Bytes Class Numeric 3x3 72 double array String 1x33 66 char array Grand total is 42 elements using 138 bytes
Note
For an example of a Windows stand-alone program (not MAT-file specific), see engwindemo.c in the <matlab>\extern\examples\eng_mat directory.
|
Reading a MAT-File in C | Reading a MAT-File in Fortran |
© 1994-2005 The MathWorks, Inc.