writing mat file from fortran

3 views (last 30 days)
rob
rob on 24 Jul 2014
Edited: James Tursa on 7 Jan 2017
First of all this is not a mex file. I have complicated type variables in fortran with hundreds of fields that i would like to write to a mat file. The only way i know to do this is to first create a matlab structure using mxCreateStructArray, then individually populate each field of this structure with each field of the fortran type variable. So the way i do it now involves a lot of repetitive coding.
Is there a better more generic way to do this? Ideally it would be nice to write a fortran type variable directly to the mat file using a function that makes the necessary conversions, but i'm guessing this isn't possible.
Or is it possible to loop through a fortran type variable of unknown number and types of fields?
Here is an example of the variable i'd like to write:
TYPE exampletype
CHARACTER*300 c1
REAL*8 r1,r2,r3
INTEGER i1,i2,i3
REAL*8, ALLOCATABLE :: ra1(:)
REAL*8, ALLOCATABLE, DIMENSION(:,:) :: ra2,ra3
REAL*8, DIMENSION(17) :: r4
LOGICAL L1
LOGICAL L2
TYPE(othertype) t1
ENDTYPE exampletype
thanks.
  1 Comment
dpb
dpb on 24 Jul 2014
There isn't a general-purpose routine that will scan the content of a UDT automagically, no. It'll require writing the specific members of a UDT as you've outlined.
It might be as straightforward if you're using (and the compiler implements it) a defined output procedure (F2008 Section 9.6.4.8.3) and essentially serialize the output as unformatted stream which could be written/read from Matlab as well rather than working to build the .mat file.

Sign in to comment.

Answers (1)

Joshua Hundley
Joshua Hundley on 7 Jan 2017

Categories

Find more on Fortran with MATLAB in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!