MATLAB: Storing a single in a structure is changing the data type

data typesMATLAB

I am optimising the way I am reading in data from a 24-bit AD converter
When the data is first read into MATLAB through a driver, it still holds as a single.
However, once stored in a struct, using whos it is now a double.
As far as I can see, a struct should be able to store multiple data types, is there a way to force it to stay as a single?

Best Answer

The data type is converted, if the field existed before and was filled with the data:
S = struct([]);
S.a = []; % Type: double
S.a(1:10) = single(1:10); % Implicit conversion to double.
% But:
S.b(1:10) = single(1:10); % S.b is a single, because it was
% a single at the creation
This means, that your expectations are correct: The fields of a struct can contain data of different type. But this detail does not hit the point: "force it to stay as a single" - the code you have written forces it to be converted to double. So please post the relevant part of the code or a minimal working example to reproduce your observation.