0

This is suppose of be very simple but I just can't get it right, probably because I don't understand something very basic.

I'm trying to figure out how to read a binary file into matlab which consists of 7 variables, each variable is written in 2 bytes (Hi and low). the first byte is MSB and others LSB. The thing is that I'm aware of fread function, but I never seem to know how to use it right when a multiple varibales are involved (just a vector of numbers of the same type are easy) so if some of you could just explain in a word or two I would be greatful.

Thank you,

1 Answer 1

2

I guess you have to call fread twice, specifying time by time which format you are using (either big-endian or little-endian).

 first = fread(fileID, 1, 'int16', 0 , 'b'); %big endian
 last =  fread(fileID, 7, 'int16', 0 , 'l'); %little endian

You have also to be specific with respect to the value type of the variable stored. You say that you have 2 bytes variables, they can be 'int16', for instance. Consider the reference of fread for the possible value types with 2 bytes.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Acorbe, the thing is this information I don't have so I have to guess, by evaluating the results. I'll give your suggestion a try.
here is the reference. My suggestion: try to read the first variable first. The types of variables featuring 2 bytes are not a lot.
Or simply call fread once, and then swap the byte values of the first variable.
@slayton, you're right, there are not 2byte floating point variables, thus the swapping is pretty easy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.