简体   繁体   English

Matlab和Fortran之间的数据解释

[英]Data interpretation between Matlab and Fortran

I have some Fortran code that interprets a binary file. 我有一些解释二进制文件的Fortran代码。 Thanks to another question I asked I understand how the fortran code works but I need help changing it to MATLAB code. 由于另一个问题,我问我了解了fortran代码的工作原理,但是我需要将其更改为MATLAB代码的帮助。 Here is the Fortran code: 这是Fortran代码:

  IMPLICIT NONE

  DOUBLE PRECISION      SCALE

  CHARACTER*72          BLINE
  CHARACTER*72          DATA_FILE_FULL_NAME
  CHARACTER*6           DATA_FILE_NAME

  CHARACTER*4           CH4, CH4F
  REAL*4                RL4
  EQUIVALENCE          (RL4,CH4)

  CHARACTER*2           C2
  LOGICAL               LFLAG

  INTEGER*2             I2

  if(i2.eq.13881) LFLAG=.FALSE.

  c2='69'
  LFLAG=.TRUE.

  DATA_FILE_FULL_NAME='./'//DATA_FILE_NAME//'.DAT'

  OPEN(UNIT=20, FILE=DATA_FILE_FULL_NAME, ACCESS='DIRECT',
 .     RECL=72, status='OLD')
    READ(20,REC=1) BLINE
    CH4f=BLINE(7:10)
    call flip(4,lflag,ch4f,ch4)
    SCALE=RL4

  RETURN
  END  

c   ..................................................
    subroutine flip(n,lflag,ch1,ch2)
c   ..................................................

  integer*4        n, i
  character*(*)    ch1, ch2
  logical          lflag

  if(lflag) then
    do i=1,n
      ch2(i:i)=ch1(n-i+1:n-i+1)
    enddo
  else
    ch2=ch1
  endif

  return
  end   

So basically (and I believe I understand this correctly) the Fortran code is checking the endianess and flipping it if the machine and the data don't match. 因此,基本上(我相信我理解正确),Fortran代码正在检查字节序,并在机器和数据不匹配时翻转字节序。 Additionally, the data is stored in memory in a place reserved for both CH4 and RL4 so by calling RL4 after defining CH4 the data is simply being cast to the REAL*4 type instead of the CHARACTER*4 type. 此外,数据存储在内存中,同时为CH4和RL4保留,因此在定义CH4之后通过调用RL4,可以将数据简单地转换为REAL * 4类型而不是CHARACTER * 4类型。 Now I need to figure out how to port this into Matlab. 现在,我需要弄清楚如何将其移植到Matlab中。 I already have the capability to read in the raw data, but when I try various forms of interpreting it I always get the wrong answer. 我已经可以读取原始数据,但是当我尝试各种形式的解释时,总是会得到错误的答案。 So far I have tried: 到目前为止,我已经尝试过:

fid=fopen(LMK_file,'r');
BLINE=fread(fid, 72,'char=>char');
CH4=BLINE(7:10);
SCALE=single(CH4(1)+CH4(2)+CH4(3)+CH4(4));

SCALE=int8(CH4(1)+256*int8(CH4(2))+256^2*int8(CH4(3))+256^3*int8(CH4(4));

in MATLAB but both give me the same wrong answer (which makes sense since its doing pretty much the same thing). 在MATLAB中,但两者都给了我相同的错误答案(这很有意义,因为它的作用几乎相同)。

Any suggestions? 有什么建议么?

For anyone else who is looking for the answer to this question this is what I came up with thanks to the help in the comments. 对于正在寻找该问题答案的其他人,这是我想出的一切,这要归功于评论中的帮助。

fid=fopen(LMK_file,'r');  %open the file for reading
IGNORE=fread(fid,6,'*char');  %ignore the first 6 bytes.  This could also be performed using fseek
SCALE=fread(fid,1,'*float32','b');  %reading in the scale value as a floating point 32 bit number. 

where the b indicates the Big endianess of the file. 其中b表示文件的大结尾。 By using the '*float32' option, fread automatically interprets the next 4 characters as a 32 bit floating point number. 通过使用'* float32'选项,fread自动将接下来的4个字符解释为32位浮点数。

This returns the correct value of scale from the given file. 这将从给定文件返回正确的比例值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM