skip data when reading binary
34 次查看(过去 30 天)
显示 更早的评论
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
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?
回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Text Data Preparation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!