Jump to content

reading a data file with MATLAB

Recommended Posts

Hi everyone,


please can anyone help me with this,


I need to read a matix which is stored in data file,

here is how I generate the matrix from FORTRAN

do i=1:500

do j=1:500

write(*,*) X(i,j)

end do

end do

the problem I do not how to read the matrix from the data file using MATLAB,

i want to know how to read the data file and then stored in a matrix,


below what I did to read the element form the data file; then stored in X(i,j)


fid = fopen('fort.dat', 'r');

for i=1:500

for j=1:500

X(i,j) = fscanf(fid, '%g );



here is the error message after run these lines..

[Fatal Error] :-1:-1: Premature end of file.

??? Error: File: PlotContour.m Line: 12 Column: 30

A MATLAB string constant is not terminated properly.

Link to comment
Share on other sites

  • 2 months later...

Looks to me like your string isn't terminated properly (as the error message says): the line

X(i,j) = fscanf(fid, '%g );

should probably be

X(i,j) = fscanf(fid, '%g');


I've used Octave to read in matrices with three columns, and they can be done in one command:

data = fscanf(fid, '%f %f %f', [3 inf])';

maybe there's a neater way to read in a 500x500 array, I'm not sure.


PS/ Sorry for the delay in replying!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...