How to best convert excel column of data ranging from -1 to 1 to a binary file ranging from 0 to 16383 with 16 bit binary

2 views (last 30 days)
I have a spreadsheet with a long column of data ranging from -1 to 1. I want to convert that, first, to an array, or column, of data ranging from 0 to 16383. Then, I want to create a binary file with those numbers as 16 bit binary numbers in the big endian format. In other words, -1 would become 0, which would ultimately be 00000000000000000. 16383 would, ultimately, be in the binary file as 0011111111111111. What is the best way to do that with Matlab? I am using v2016B.

Answers (2)

Image Analyst
Image Analyst on 24 Sep 2016
Try mat2gray():
m = 2 * rand(10,3) - 1 % Sample data
maxUint16Value = double(intmax('uint16'))
mScaled = uint16(maxUint16Value * mat2gray(m))
mat2gray() is in the Image Processing Toolbox.

Walter Roberson
Walter Roberson on 24 Sep 2016
Edited: Walter Roberson on 24 Sep 2016
xmap = uint16( floor((x+1)*32767/2) ) %discards any fraction
or
xmap = uint16( (x+1)*32767/2 ); %round any fraction
Now to write it in bigendian:
fid = fopen('TheFileName.bin', 'w');
fwrite(fid, x, 'uint16', 'ieee-be'); %write big-endian
fclose(fid);
The default for writing is "native", which is whatever format the processor happens to operate with. For all systems currently supported, "native" happens to correspond to little-endian.

Community Treasure Hunt

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

Start Hunting!