Reading multi-point binaries via fread in matlab

I have a huge binary file that has entries with several dots like {"Double", "Double", 'Int32', 'INT8', 'Char'}. I used memmapfile to read in data, but its painfully slow to read in data. Is there a way to read the whole file through fread?

+5
source share
1 answer

You can use 'skip'the FREAD function option , as well as FSEEK , to read records one column at a time:

%# type and size in byte of the record fields
recordType = {'double' 'double' 'int32' 'int8' 'char'};
recordLen = [8 8 4 1 1];
R = cell(1,numel(recordType));

%# read column-by-column
fid = fopen('file.bin','rb');
for i=1:numel(recordType)
    %# seek to the first field of the first record
    fseek(fid, sum(recordLen(1:i-1)), 'bof');

    %# % read column with specified format, skipping required number of bytes
    R{i} = fread(fid, Inf, ['*' recordType{i}], sum(recordLen)-recordLen(i));
end
fclose(fid);

, . , .

+7

All Articles