title: reading unformatted fortran file in matlab - which precision?
body: <p>I have just written out a file:</p>
  
  <pre><code> real*8                         :: vol_cel
   real*8, dimension(256,256,256) :: dense
  
  [... some operations]
  
  open(unit=8,file=fname,form="unformatted")
  write(8)dense(:,:,:)/vol_cell
  close(8)
  </code></pre>
  
  <p>My code to read this in in Matlab:</p>
  
  <pre><code>fid = fopen(fname,'r');
  mesh_raw = fread(fid,256*256*256,'double');
  fclose(fid);
  </code></pre>
  
  <p>The min and max values clearly show that it is not reading it in correctly (Min is 0 and max is a largish positive real*8). </p>
  
  <pre><code>min =
     3.3622e+38
  max =
    -3.3661e+38
  </code></pre>
  
  <p>What precision do I need to set in Matlab to make it read in the unformatted Fortran file?</p>
  
  <p>A somewhat related question: This Matlab code I am using reads binary files OK but not unformatted files. Though I am generating this new data on my Mac OSX using gfortran. It doesn't recognize form="binary" so I can't do it that way. Do I need to add some library or this an endian problem?</p>
  
  <p>===== Progress =====</p>
  
  <p>OK progress. Instead of my ndim*ndim*ndim matrix I just wrote out the x values (column vector) as such:</p>
  
  <pre><code>open(unit=8,file=fnamex,form="unformatted")
  write(8)x0
  close(8)
  </code></pre>
  
  <p>Then Matlab reads:</p>
  
  <pre><code>fid =    fopen(nfilename,'r');
  hr3=fread(fid, 1, 'int32');
  x0 = fread(fid,Ntot,'float32');
  hr4=fread(fid, 1, 'int32');
  fclose(fid);
  </code></pre>
  
  <p>THAT worked. Then I tried the original ndim**3 matrix, I tried to read with: </p>
  
  <pre><code>fid =    fopen(nfilename,'r');
  hr3=fread(fid, 1, 'int32');
  mesh_raw = fread(fid,ndim*ndim*ndim,'float32');
  hr4=fread(fid, 1, 'int32');
  fclose(fid);
  </code></pre>
  
  <p>But that gives me garbage. Perhaps here:</p>
  
  <pre><code>real*4,     dimension(:),   allocatable    :: x0
  real*8,     dimension(256,256,256)         :: dense
  </code></pre>
  
  <p>Do I need to change: mesh_raw = fread(fid,ndim*ndim*ndim,'float32'); to make sure it is reading a real*8? What would that be? Surely just using 'real*8' verbatim should work? I mean 'real*4' for the x vector works. I mean it reads "dense" but the min/max/average values are wrong.</p>