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 );

end

end

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...