deteriming number of bytes in one line of binary file
13 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I am working on translating some old fortran code into MATLAB and have run across the following issue.
In the fortran file I am working with the following check is performed
CHARACTER*72 PICTFILE
CHARACTER*8192 INLINE
INTEGER NPX
INTEGER NLN
INTEGER BYTES
c This is read from another file but I'll just hard code it for now
NPX = 1024
NLN = 1024
bytes=2
open(unit=10, file=pictfile, access='direct', recl=2*npx, status='old')
read(10,rec=nln, err=20) inline(1:2*npx)
go to 21
20 bytes=1
21 continue
close(unit=10)
where nln is the number of lines in the file being read, and npx is the number of integers contained in each line. This check basically determines whether each of those integers is 1 byte or 2 bytes by trying to read in a line of 2 bytes for each integer, and then adjusting to 1 byte if there is an error. I understand the fortran code well enough to figure that out, but now I need to figure out how to perform this check in MATLAB. I have tried using the fgetl command on the file and then reading the length of the characters contained but the length never seems to be more than 4 or 5 characters, when even if each integer is 1 byte the length should be somewhere around 1000.
Can someone help me with devising some way to perform this check in MATLAB? I've thought about maybe a try-catch statement but I'm not very familiar with them and don't really know where to start.
Thanks! Andrew
7 件のコメント
採用された回答
Andrew
2014 年 6 月 9 日
編集済み: Andrew
2014 年 6 月 9 日
1 件のコメント
dpb
2014 年 6 月 9 日
... if all of the data in the file is the same type (ie uint8 or uint16) and you know the number of entries that should be contained in the file beforehand.
If that were the case you should be able to just find the SIZE in bytes and divide it is stream ('binary') data only.
It still doesn't make sense unless there was a compiler switch that made the Fortran unformatted record look like a stream file, though, altho apparently it was...does GFortran actually read it correctly w/o any special switch settings? I've not used it enough to know its handling otomh but it's plenty weird if it does.
But, I suppose "all's well that end's well"... :)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で National Instruments Frame Grabbers についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!