skip data when reading binary

조회 수: 29 (최근 30일)
Shane
Shane 2016년 7월 1일
댓글: Image Analyst 2016년 7월 1일
I have a binary data file, and I am reading it as such.
fileID = fopen('2016.06.27_15.12.07_1.A.bin');
SDDdata = fread(fileID, 1E6, 'uint16' , 'ieee-le');
fclose all;
However, I want to skip every other data point, or every 3, etc. As the file is incredibly large and I want to to some testing with ignoring data. Looking up the fread command I can see there is a skip options, but I could not get it to work for this? It seemed to do nothing?
  댓글 수: 2
James Tursa
James Tursa 2016년 7월 1일
Please show the code you are using for skipping. Are you sure you are inputting the number of bytes to skip and not the number of values?
Walter Roberson
Walter Roberson 2016년 7월 1일
Good point about it being in bytes .

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2016년 7월 1일
In R2016b I tested by writing out uint8(0:255) repeatedly, and then doing several tests
bytes = uint8(0:255);
fid = fopen('testfread.bin', 'w');
for K = 1 : 4096; fwrite(fid, bytes, 'uint8'); end
fclose(fid);
fid = fopen"testfread.bin', 'r');
skip = 0; frewind(fid); data0 = fread(fid, [1 100], 'uint8', skip, 'ieee-le')
skip = 1; frewind(fid); data1 = fread(fid, [1 100], 'uint8', skip, 'ieee-le')
skip = 2; frewind(fid); data2 = fread(fid, [1 100], 'uint8', skip, 'ieee-le')
fclose(fid)
The output was exactly what I expected: first consecutive bytes [0 1 2 3 ...] for skip 0, then every second byte [0 2 4 6 ...] for skip 1, then every third byte [0 3 6 9 ...] for skip 2. Skip seems to work correctly on my system.
  댓글 수: 2
James Tursa
James Tursa 2016년 7월 1일
Worked for me too, which is why I want OP to post the exact code being used.
Image Analyst
Image Analyst 2016년 7월 1일
Why didn't you try it for uint16? for completeness, I do that below:
% Create sample data file.
bytes = uint16(0:255);
fid = fopen('testfread.bin', 'w');
for K = 1 : 4096; fwrite(fid, bytes, 'uint16'); end
fclose(fid);
% Read it back in.
fid = fopen('testfread.bin', 'r');
numbersToSkip = 0;
bytesToSkip = numbersToSkip * 2; % Multiply by 2 for uint16, which is 2 bytes
frewind(fid);
data0 = fread(fid, [1 100], 'uint16', bytesToSkip, 'ieee-le')
numbersToSkip = 1;
bytesToSkip = numbersToSkip * 2; % Multiply by 2 for uint16, which is 2 bytes
frewind(fid);
data1 = fread(fid, [1 100], 'uint16', bytesToSkip, 'ieee-le')
numbersToSkip = 2;
bytesToSkip = numbersToSkip * 2; % Multiply by 2 for uint16, which is 2 bytes
frewind(fid);
data2 = fread(fid, [1 100], 'uint16', bytesToSkip, 'ieee-le')
fclose(fid)
It's a little bit trickier since you have to multiply the number to skip by 2 since there are two bytes in a uint16 number.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by