Use fread for binary data. Following your description, probably:
fid = fopen(filepath, 'r', 'l', 'ISO-8859-1');
signal.name = fread(fid, [1 18], '*char');
signal.value = fread(fid, 1, 'int16');
signal.date = fread(fid, 1, 'int32');
signal.milliseconds = fread(fid, 1, 'int16');
signal.status = fread(fid, 1, '*uint8');
signal.type = fread(fid, 1, 'uint8');
For reading multiple records you can wrap the above in a loop. It won't be very fast though. Another option is to use the skip argument of fread to all the names in one go, rewind the file, read all the values, rewind the file, etc..:
fid = fopen(filepath, 'r', 'l', 'ISO-8859-1');
signal.names = fread(fid, [18 Inf], '*char', 28)';
fseek(fid, 18, 'bof');
signal.values = fread(fid, [1 Inf], 'int16', 28)';
fseek(fid, 20, 'bof');
signal.date = fread(fid, [1 Inf], 'int32', 28);
Possibly, the fastest option may be to read the whole file in one go and perform the conversion afterward:
recordfields = {'name', 'value', 'date', 'milliseconds', 'status', 'type'};
recordtypes = {'char', 'int16', 'int32', 'int16', 'uint8', 'uint8'};
recordsizes = {18, 2, 4, 2, 1, 1};;
fid = fopen(filepath, 'r', 'l', 'ISO-8859-1');
data = fread(fid, [sum(recordsizes), Inf], '*uint8')';
data = mat2cell(data, size(data, 1), recordsizes);
data = cellfun(@(col, type) typecast(col, type), data, recordtypes, 'UniformOutput', false);
record = cell2struct(data, recordfields, 2);
0 Comments
Sign in to comment.