Dave
Dave

Reputation: 8109

What is the conventional type for representing boolean values in Matlab?

I'm reading binary data from a file, the particular value in question is a uchar (unsigned 1-byte value) and conceptually is boolean. How do I write the fread statement?

I.e. what should I put in for ??? in the following:

is_valid=fread(fid, 1, 'uint8=>???','a');

I figure that I could use '*uint8' for the conversion string, but I'd like for the result type to be most-like what other users would expect.

Upvotes: 0

Views: 263

Answers (2)

Randi
Randi

Reputation: 330

This works on 2012b:

A = fread(fid, 1, 'uint8=>logical');

Upvotes: 0

Eitan T
Eitan T

Reputation: 32930

Booleans in MATLAB are represented by the logical type. However, fread doesn't support reading logicals, so read them as uint8 and convert to logical later. For instance:

is_valid = logical(fread(fid, 1, 'uint8', 'a'));

Upvotes: 2

Related Questions