Stephni Posted July 17, 2010 Report Share Posted July 17, 2010 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. Quote Link to comment Share on other sites More sharing options...
neddie Posted October 5, 2010 Report Share Posted October 5, 2010 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.