Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to read an n-dimensioned matrix from a binary file

Subject: How to read an n-dimensioned matrix from a binary file

From: Jim

Date: 13 Feb, 2013 15:54:12

Message: 1 of 3

I would like to write a Matlab script to read an N-dimensioned matrix created in Fortran. Specifically, the Fortran code wrote to a binary file using the following:

write(30) a, b, c

with the following declarations for a,b,c

real a(16,20,22,6,3, 2)

real b(16,20,22,6,3,13)

real c(16,20,22,6,3,13)

In Matlab, it seems that fread handles only a vector or 2-dimesional matrix
fread(fileID, sizeA) where sizeA is a vector of size n or 2-dimensional matrix [m,n].

How can I read the N-dimensioned matrix I described?

Subject: How to read an n-dimensioned matrix from a binary file

From: James Tursa

Date: 13 Feb, 2013 16:28:14

Message: 2 of 3

"Jim " <psugrad1990@gmail.com> wrote in message <kfgcv4$61l$1@newscl01ah.mathworks.com>...
> I would like to write a Matlab script to read an N-dimensioned matrix created in Fortran. Specifically, the Fortran code wrote to a binary file using the following:
>
> write(30) a, b, c
>
> with the following declarations for a,b,c
>
> real a(16,20,22,6,3, 2)
>
> real b(16,20,22,6,3,13)
>
> real c(16,20,22,6,3,13)
>
> In Matlab, it seems that fread handles only a vector or 2-dimesional matrix
> fread(fileID, sizeA) where sizeA is a vector of size n or 2-dimensional matrix [m,n].
>
> How can I read the N-dimensioned matrix I described?

Cross-posted on Answers. See my comments there ...

James Tursa

Subject: How to read an n-dimensioned matrix from a binary file

From: dpb

Date: 13 Feb, 2013 18:00:50

Message: 3 of 3

On 2/13/2013 9:54 AM, Jim wrote:
> I would like to write a Matlab script to read an N-dimensioned matrix
> created in Fortran. Specifically, the Fortran code wrote to a binary
> file using the following:
>
> write(30) a, b, c
> with the following declarations for a,b,c
>
> real a(16,20,22,6,3, 2)
>
> real b(16,20,22,6,3,13)
>
> real c(16,20,22,6,3,13)
>
> In Matlab, it seems that fread handles only a vector or 2-dimesional matrix
> fread(fileID, sizeA) where sizeA is a vector of size n or 2-dimensional
> matrix [m,n].
>
> How can I read the N-dimensioned matrix I described?

Need more info...did the Fortran program write a Fortran unformatted
file as a default OPEN(lun,form='unformatted')? If so, there's a record
structure that will have to be dealt with (Fortran unformatted files are
_NOT_ C-like stream files but have internal record information to
support operations like BACKSPACE). You'll have to read that
information as well.

If, otoh, either a pre-F2003 extension was used (supported by every
compiler I'm aware of in one form or another) or the F2003+ Standard
mechanism has been implemented in the compiler and used, then all you
need to do is to read the file and reshape since both Fortran and Matlab
use column-major storage order.

--

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us