Read binaries from matlab 
Author Message
 Read binaries from matlab

Hello,

I have written data by matlab on a file, using fwrite function.
I want read them by fortran. I know how to read formated file (which
can be created by matlab's fprintf).
But I don't know how read binaries created by matlab.

Is there anyway to do that?

Thanks a lot

Farhad



Tue, 11 May 2010 00:19:07 GMT  
 Read binaries from matlab

Quote:

> Hello,

> I have written data by matlab on a file, using fwrite function.
> I want read them by fortran. I know how to read formated file (which
> can be created by matlab's fprintf).
> But I don't know how read binaries created by matlab.

> Is there anyway to do that?

With most Fortran compilers, yes.  It requires an extension to be able
to open the file for what is often called "stream" or "binary" i/o
rather than as a Fortran unformatted file (which contains record markers
in addition to the data).

Since it is an extension, check for such an option in your particular
compiler's OPEN statement documentation and for a discussion in the
programmer's guide for i/o.

--



Tue, 11 May 2010 00:27:59 GMT  
 Read binaries from matlab

Quote:
> Hello,

> I have written data by matlab on a file, using fwrite function.
> I want read them by fortran. I know how to read formated file (which
> can be created by matlab's fprintf).
> But I don't know how read binaries created by matlab.

> Is there anyway to do that?

> Thanks a lot

> Farhad

Hello: At least for idl I do it as follows: in idl I write out the
dimension of the vector or array and then the arry itslef. idl has the
option to writte binaries in F77 style (you should check if that is
possible with matlab too).

e.g. in idl I write (dimension and array):

==
    openw,lun,(erg_dir+'tes_species.' + filesn(i) + '.bin'),/get_lun,/
f77_unformatted,/swap_endian
      writeu,lun,(size(species_data.species))(1)
      writeu,lun,(size(species_data.species))(2)
      writeu,lun,species_data.species
    free_lun,lun
==

In Fortran I read in:

==
open(unit=5,file=(trim(tes_co_path)//'tes_pressure.' //dummy_year//&
     '.'//dummy_month//'.'//dummy_day//'.bin'), form='unformatted',
status='old')

  read(5) dim
  read(5) dim2
  read(5) buff2d(1:dim,1:dim2)
close(5)
pressure = buff2d(:,:)*1.0
==

dim, dim2 are the dimensions (integer) and buff2d has been initialsed
by a huge dummy array buffer (real*4, dimension(buff1,buff2) :: buff2d)



Tue, 11 May 2010 19:32:18 GMT  
 Read binaries from matlab
Quote:


>> Hello,

>> I have written data by matlab on a file, using fwrite function.
...
> Hello: At least for idl I do it as follows: in idl I write out the
> dimension of the vector or array and then the arry itslef. idl has the
> option to writte binaries in F77 style (you should check if that is
> possible with matlab too).

...

Not a Matlab option (at least w/o mimicking the Fortran unformatted file
structure manually).  In Matlab, fwrite() writes only the data (in
column order, incidentally, which is handy) so to read files created
that way one needs the "stream" i/o extension of F90/95 or the '03
version implemented.   AFAIK any current compiler has some way to
accomplish this, albeit w/ some compiler-specific options.

--



Tue, 11 May 2010 22:59:01 GMT  
 Read binaries from matlab


Quote:
>Hello,

>I have written data by matlab on a file, using fwrite function.
>I want read them by fortran. I know how to read formated file (which
>can be created by matlab's fprintf).
>But I don't know how read binaries created by matlab.

>Is there anyway to do that?

>Thanks a lot

>Farhad

Would it be possible for you to write a simple c routine to read in
the data and call that from your Fortran code? Another possibility
would be to create an engine application under MATLAB and pass the
data into your Fortran code that way.

James Tursa



Wed, 12 May 2010 09:33:03 GMT  
 Read binaries from matlab
Quote:



>> Hello,

>> I have written data by matlab on a file, using fwrite function.
>> I want read them by fortran. I know how to read formated file (which
>> can be created by matlab's fprintf).
>> But I don't know how read binaries created by matlab.

...

Quote:
> Would it be possible for you to write a simple c routine to read in
> the data and call that from your Fortran code? Another possibility
> would be to create an engine application under MATLAB and pass the
> data into your Fortran code that way.

That's one way, but certainly the direct route is simpler.

If OP would indicate which compiler is his choice, undoubtedly someone
here could point directly to the right incantation...

For CVF, Form='Binary' in the OPEN is it--

--



Wed, 12 May 2010 09:44:37 GMT  
 Read binaries from matlab
Thank all you guys for so many answers. I am really happy for that. I
must say that
I am not so good programmer, so many of things you have said were
difficult to
understand.

However I tried following code:

  open(unit=1,file='test.bin',form='unformatted')
  read(1) A
  close(1)

But it did not work. There were no problem with compilation, but when
I get following runtime error messages :

in g95:
    Traceback: not available, compile with -ftrace=frame or -
ftrace=full
    Fortran runtime error: Corrupt record in unformatted sequential-
access file

in gfortran:
    Fortran runtime error: End of record

Thanks again

Farhad



Thu, 13 May 2010 02:10:09 GMT  
 Read binaries from matlab

Quote:

>   open(unit=1,file='test.bin',form='unformatted')
...
> But it did not work.

That's right. It won't. As was said, plain unformatted I/O will not work
for what you want. That's what this is.

Quote:
> in g95:

G95 supports f2003 stream I/O. Just add access='stream' in the above
OPEN statement.

Quote:
> in gfortran:

I don't recall whether gfortran supports stream or not. If it doesn't
support stream, it is almost bound to support one of the common
nonstandard synonyms.

--
Richard Maine                    | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle           |  -- Mark Twain



Thu, 13 May 2010 02:41:14 GMT  
 Read binaries from matlab

Quote:

>>   open(unit=1,file='test.bin',form='unformatted')
> G95 supports f2003 stream I/O. Just add access='stream' in the above
> OPEN statement.

>> in gfortran:

> I don't recall whether gfortran supports stream or not.

It does.  Add access="stream", and it should work.


Thu, 13 May 2010 02:48:25 GMT  
 Read binaries from matlab
Thank you all guys! :-), :-), :-)
Now everything works.

I tried as well to write binaries by fortran with access='stream',
and then I read them from matlab, and it worked well too.

Thank you again

Farhad



Thu, 13 May 2010 03:34:11 GMT  
 Read binaries from matlab


Quote:
>Thank you all guys! :-), :-), :-)
>Now everything works.

>I tried as well to write binaries by fortran with access='stream',
>and then I read them from matlab, and it worked well too.

>Thank you again

>Farhad

Follow up information for other readers:  

For the Intel Fortran 9.1 compiler, opening with the following option
works:

   form='binary'

(binary was an option suggested by a previous poster ... this is not
my original idea)

You just need to be careful on the MATLAB side when using fwrite to
make sure you are specifying the precision so that it matches the
Fortran side. For instance, this will not work even though A is a
double having exactly the desired floating point bit pattern for a
real*8 on the Fortran side:

MATLAB side:

Quote:
>> A = 1:10;
>> fid = fopen('data.bin', 'w')
>> fwrite(fid, A)
>> fclose(fid)

Fortran side:

      real*8 A(10)
      open(unit=1,file='data.bin',form='binary',action='read')
      read(1) A

The reason this will not work is because the fwrite(fid, A) statement
will convert the double variable A into a uint8 type (unsigned 8-bit
integer) and write that out, putting only 10 bytes of integer data
into the file. The Fortran program will attemp to read 80 bytes of
floating point data and get an EOF on the attempt.  To avoid this you
need to specify precision on the MATLAB side for the fwrite operation:

Quote:
>> fwrite(fid, A, 'double')

There are also options on the MATLAB side for machine format, etc.

James Tursa



Sun, 16 May 2010 13:18:26 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Reading binary files, unflatten binary string

2. Reading a binary file / writing binary data to a file

3. How do I write binary data in LabVIEW to a Matlab MAT-file

4. FORTRAN BINARY FILE COMPATIBILITY W/ MATLAB

5. Reading Arguments from Command Line and reading binary data

6. Reading GIF in Labview via MATLAB

7. Reading fortran namelist files into matlab

8. Read/Write Matlab files in fortran

9. Read Matlab files with Python and Numeric?

10. Matlab Script and Matlab to HIQ

11. binary file read/write acces

12. Reading binary files with DOS driver

 

 
Powered by phpBB® Forum Software