Hi guys,
I have to read from a binary file and transform to decimal for example the first 8 groups at 8 bit level and the folowing 7 groups at 56 bit level as integer.
I tried this :
fileID = fopen('test.bin');
precision = '1*uint8';
B = fread(fileID,[1,1],precision);
%% I know this read only the first group and its mean 0 but for example I want to skip the first 8 groups and read after them the following 7 at 56 bit level
%% And after that I want to read specific groups at the same 56 bit level
%% I think I made my self clear otherwise please ask me if something that you dont understand.
Thanks!

 Accepted Answer

Try this
fileID = fopen('test.bin');
precision = '1*uint8';
fseek(fileID, 8, 0); % skip 8 bytes
B = fread(fileID,[1,7],precision); % read 7 bytes

11 Comments

fseek(fileID, 8, 0);
So this line skip the first 8 bytes, ok?
%% But for me to read 56 bits I need to modify the precision ?
precision = '7*ubit56'; % like this?
You can just use
precision = '*ubit56';
fread(fileID,1,precision)
7*ubit56 is only needed if you are going to use the skip argument to fread().
fileID = fopen('test.bin');
precision = '*ubit56';
fseek(fileID, 8, 0); % skip 8 bytes
A = fread(fileID,1,precision);
This works fine, but now I want to search more multiple 56 bit in the bin file and read all, all I now is that the after this 7 group of 56 bit i have to skip always another 8 gropus of 8 bit and again read the data in 56 bit.
How can I write this ?
Thanks!
Once the initial 8 bytes are skipped, you can use the skip argument of fread() to skip a blocks of 8 bytes after reading every seven bytes
f = fopen('test.bin', 'r');
precision = '*ubit56';
fseek(f, 8, 0); % skip 8 bytes
A = fread(f,inf,precision, 8*8) % skips is in number of bits
fclose(f);
A is an array and it 'inf' aregument tells fread() to read till end of the file.
Works perfect, Thanks Hamza!
I am glad to be of help!
One more think Amza, do you think it's possible to run the script continuously, because my data in the file are always updated? I'd figure to use run command but I don't know how to apply a continuous mode which would offer in a different window the results. For that I will use probably output command.
Can you exaplain what do you mean by continuously running the script. Do you want to run it at regular intervals?
Yes, for example I want to gather data from a file which data's are updated continuasly, let's say I want to run the above code for 20sec after that a pause of 5sec and runnit again, but would be preferable the code to run from the stopped point and not to run from the beginning of the file.
I haven't experimented with reading the file, which is also being updated. You need to do some tests whether it works correctly and next fread() will read the new data correctly. For periodic execution of the code, check timers.
It's working I already tried and it's OK, the result is updated every time I hit Run. But I guess I have Tu figure how is with the timers... Thanks...

Sign in to comment.

More Answers (0)

Products

Release

R2016b

Community Treasure Hunt

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

Start Hunting!