[petsc-users] binary format difference between matlab and pestc

Barry Smith bsmith at mcs.anl.gov
Sat Feb 15 23:45:52 CST 2014


On Feb 15, 2014, at 7:59 PM, Xiangdong <epscodes at gmail.com> wrote:

> Hello everyone,
> 
> Are the binary format of petsc and matlab the same? When I run the vec ex6, petsc outputs (using PetscBinaryWrite) a binary file "input.dat" with values 
> 0
> 10
> 20
> 30
> 40
> 50
> 60
> 70
> 80
> 90
> 
> However, when I read this binary from matlab (use fopen and fread), I got all values zeros. What might be wrong here?

  Likely it is the byte swapping that takes place in saving/reading the files that is inconsistent on the two sides. You need to use fopen(filename,rw,'ieee-be’) on the Matlab side.
> 
> Do I have to use petsc functions other than PetscBinaryWrite in order to produce binary files which can be read into matlab? 

  You really, really don’t want to use the low level PetscBinaryWrite()/fopen,fread() to move vectors from PETSc to MATLAB, it won’t work in parallel and is cumbersome.  You want to use VecView() in Petsc and then bin/matlab/PetscBinaryRead.m in Matlab to read in the result.

   Barry

Note that in the end VecView() calls PetscBinaryWrite() and bin/matlab/PetscBinaryRead.m calls fopen,fread() but they handle the parallelism and byte swapping all for you.

> 
> Thank you.
> 
> Xiangdong
> 
> 
> http://www.mcs.anl.gov/petsc/petsc-current/src/vec/vec/examples/tutorials/ex6.c.html



More information about the petsc-users mailing list