Convert Fortran code to Matlab

13 次查看(过去 30 天)
Hi, I need help in converting the following Fortran code to Matlab
* This program reads binary data for 365/366 days and writes in ascii file.
PROGRAM READ
PARAMETER(ISIZ=31,JSIZ=31)
DIMENSION T(366,ISIZ,JSIZ)
OPEN(1,FILE='D:\\DailyT\\MeanT\\MEANT1980.GRD',
1 FORM='UNFORMATTED',ACCESS='DIRECT',
2 RECL=ISIZ*JSIZ*4,STATUS='OLD')
OPEN(2,FILE='D:\\DAILYT\\MEANT15APR1980.TXT',STATUS='UNKNOWN')
* TAKE NDAY=366 FOR LEAP YEARS
NDAY=366
DO IDAY = 1,NDAY
READ(1,REC=IDAY)((T(IDAY,I,J),J=1,JSIZ),I=1,ISIZ)
ENDDO
WRITE(2,'('' Daily Tempereture for 15 APR 1980 '')')
DO I = 1,ISIZ
WRITE(2,'(31F6.2)')(T(106,I,J),J=1,JSIZ)
ENDDO
STOP
END
Thanks
Shubham

采纳的回答

Walter Roberson
Walter Roberson 2020-7-3
1 FORM='UNFORMATTED',ACCESS='DIRECT',
There is no standard for the representation of DIRECT UNFORMATTED files in fortran. The most common implementation is as a binary file in which each record is proceeded and followed by a 4 byte record length count.
You should probably be doing something like
NDAY = 366;
ISIZ = 31; JSIZ = 31;
T = zeros(NDAY, ISIZ, JSIZ, 'single');
fid = fopen('D:/DailyT/MeanT/MEANT1980.GRD');
for IDAY = 1 : NDAY
fseek(fid, 4, 'cur');
T(IDAY, :, :) = fread(fid, [JSIZ ISIZ], '*single') .';
fseek(fid, 4, 'cur');
end
fclose(fid);
After which you would write the data to a file, a row at a time
fmt = [repmat('%6.2f', 1, 31), '\n'];
fid = fopen( 'D:/DAILYT/MEANT15APR1980.TXT', 'wt');
fprintf(fid, '('' Daily Tempereture for 15 APR 1980 '')\n');
fprintf(fid, fmt, permute(T(106,:,:), [3 2 1]));
fclose(fid);
  2 个评论
Walter Roberson
Walter Roberson 2020-7-3
It appears that those particular files have no gaps between records, so remove the fseek() calls.
The values of Tr must be less than 20
The -999 "no data" entries are coming through too clearly for me to believe that there is a byte order problem or anything like that.
If you examine the data for days 129 and (especially) 130, you will see entries as high as 133.7053 . It looks like there was probably a bad storm those days, which would correspond to May 9 to May 10 of whatever year you are examining.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Fortran with MATLAB 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by