📄 mgetbin.m
字号:
function [out] = mGetBin(fname,type,format,endian)
% Reads integers from a binary file.
%
% out = mGetBin(fname,type,format,endian)
%
% out : column array (real or complex)
% The class of the output depends on type.
%
% fname : name of the binary output file
%
% type : defines how the input data stream is interpreted
% 'int8' signed 8-bit integers (-127 .. 128)
% 'int16' signed 16-bit integers (-32768 .. 32767)
% 'int32' signed 32-bit integers (-2147483648 .. 2147483647)
% 'uint8' unsigned 8-bit integers (0 .. 255)
% 'uint16' unsigned 16-bit integers (0 .. 65535)
% 'uint32' unsigned 32-bit integers (0 .. 4294967295)
%
% format : defines how the data is arranged in the binary file
% 'R' real values only
% 'I' imaginary values only
% 'RI' first all real, second all imaginary
% 'IR' first all imaginary, second all real
% '1R1I' 1 real and 1 imaginary value, alternating
% '4R4I' 4 real and 4 imaginary values, alternating
% '8R8I' 8 real and 8 imaginary values, alternating
%
% endian : big 'b' or little 'l', default is 'b'
%
% Within this function the input is always rounded to convert the
% double values into integers.
%
% Examples:
%
% Binary file:
% 02 fe 38 0c 7f 7f 00 7f 55 7f 13 d5 a7 94 f1 ca
% 54 7b 12 38 bc ef d9 32 f3 56 67 ea a9 b1 92 05
%
% mGetBin('file.bin','int8','R') generates the output
% 2 -2 56 12 127 127 0 12 (1..8)
% 85 127 19 -43 -89 -108 -15 -54 (9..16)
% 84 123 18 56 -68 -17 -39 50 (17..24)
% -13 86 103 -22 -87 -79 -110 5 (25..32)
%
% mGetBin('file.bin','uint8','I') generates the output
% 0 + 2i 0 + 254i 0 + 56i 0 + 12i (1..4)
% 0 + 127i 0 + 127i 0 + 0i 0 + 127i (5..8)
% 0 + 85i 0 + 127i 0 + 19i 0 + 213i (9..12)
% 0 + 167i 0 + 148i 0 + 241i 0 + 202i (13..16)
% 0 + 84i 0 + 123i 0 + 18i 0 + 56i (17..20)
% 0 + 188i 0 + 239i 0 + 217i 0 + 50i (21..24)
% 0 + 243i 0 + 86i 0 + 103i 0 + 234i (25..28)
% 0 + 169i 0 + 177i 0 + 146i 0 + 5i (29..32)
%
% mGetBin('file.bin','int16','1R1I') generates the output
% 766 + 14348i 32639 + 127i 21887 + 5077i -22636 - 3638i (1..4)
% 21627 + 4664i -17169 - 9934i -3242 + 26602i -22095 - 28155i (5..8)
%
% mGetBin('file.bin','uint16','8R8I','l') generates the output
% 65026 + 31572i 3128 + 14354i 32639 + 61372i 32512 + 13017i (1..4)
% 32597 + 22259i 54547 + 60007i 38055 + 45481i 51953 + 1426i (5..8)
%
% See also mGenBin
%
% Matlab 7 Release 14 SP2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Property of Freescale
% Freescale Confidential Proprietary
% Freescale Copyright (C) 2005 All rights reserved
% ----------------------------------------------------------------------------
% $RCSfile: mGetBin.m.rca $
% $Revision: 1.1 $
% $Date: Mon Dec 11 12:07:10 2006 $
% Target: Matlab
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Author: Karsten Mohr
% Date : Sep 2005
%set default endianess to big endian
if ~exist('endian','var')
endian='b';
end
%open binary file
fid = fopen(fname,'r');
if fid==-1
error('Error: Can not open file "%s".',fname);
end
switch type
case 'int8'
out = fread(fid,inf,'int8',endian);
case 'uint8'
out = fread(fid,inf,'uint8',endian);
case 'int16'
out = fread(fid,inf,'int16',endian);
case 'uint16'
out = fread(fid,inf,'uint16',endian);
case 'int32'
out = fread(fid,inf,'int32',endian);
case 'uint32'
out = fread(fid,inf,'uint32',endian);
otherwise
error('Error: Type not supported.');
end
n = length(out);
switch format
case 'R'
%do nothing
case 'I'
out = i*out;
case 'RI'
out = reshape(out,n/2,2);
out = out(:,1) + i*out(:,2);
case 'IR'
out = reshape(out,n/2,2);
out = out(:,2) + i*out(:,1);
case '1R1I'
out = reshape(out,2,n/2);
out = (out(1,:)+i*out(2,:)).';
case '4R4I'
out = reshape(out,8,n/8);
out = reshape(out(1:4,:)+i*out(5:8,:),n/2,1);
case '8R8I'
out = reshape(out,16,n/16);
out = reshape(out(1:8,:)+i*out(9:16,:),n/2,1);
otherwise
error('Error: Format not supported.');
end
switch type
case 'int8'
out=int8(out);
case 'uint8'
out=uint8(out);
case 'int16'
out=int16(out);
case 'uint16'
out=uint16(out);
case 'int32'
out=int32(out);
case 'uint32'
out=uint32(out);
end
%close binary file
fclose(fid);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -