Reading a binary file in parts

I have raw data from a sensor stored in a 1Gb binary file, and I would like to read it sequentially, i.e. in a loop, where the information is being processed simultaneously. In other words, as if the data were being received in real time. Is that possible in Matlab?

 Accepted Answer

Alexander
Alexander on 24 Sep 2023
You should use fread.

4 Comments

I have that idea, Paul, but can be used this function, fread, for reading an intermediate part of the file? How?
Alexander
Alexander on 24 Sep 2023
Moved: Voss on 24 Sep 2023
Here some code:
% ExampleFread.m
fid = fopen('ABCDE.txt');
in = fread(fid,4); % read 4 byte
char(in)
in = fread(fid,4); % read another 4 byte
char(in)
fclose(fid)
Hope it helps.
Alexander
Alexander on 24 Sep 2023
Moved: Voss on 24 Sep 2023
Just some remarks:
If you are not short of memory you can use
in = fread(fid);
Than you have the whole bunch of bits and bytes in your vector "in". After that you can parse the data according your needs.
Second: If you have to deal often with the file directly, get familiar with the low level i/o functions of Matlab (i.e. fseek, frewind, feof, ..., these are similar like the functions in C language), then you can stroll through your file as you want.
Understood. Thanks to both of you!

Sign in to comment.

More Answers (0)

Products

Release

R2020b

Asked:

on 24 Sep 2023

Commented:

on 24 Sep 2023

Community Treasure Hunt

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

Start Hunting!