|
If any of you are familiar with matlab, even basic knowledge, your help would be much appreciated because of my lack of understanding/familiarity with matlab...
What I need to do is make an m-file that can extract input from a text file, change it into 16-bit binary while being precise as possible, and store that new 16-bit binary string onto another text file. The text file that I need to extract data from comprises of negatives and decimals ie. -0.0015, 0.00654. The function used to change integers to binary is "dec2bin" and only works with non-negative integers so to solve this problem, I've added and multiplied to the string to convert them to integers. I've heard that 2's complement and LSB could be used to solve this problem but I am not familiar with either. Any explanation would be useful. Thank you in advance.
Here is what I have so far:
clear all
datastr=textread(uigetfile('*.txt','Choose Text File')); %select txt file
user_entry=input('Which column would you like to extract data from? ') %select column of data
x=datastr(:,user_entry); %extracts selected column of data
%if lowest # in string is negative, adds inverse negative # to string so
%that string becomes positive
if min(x)<=0
x_1=(x-min(x))+0.1
else
x_1=x
end
%finds value to multiply by x_1 to get the highest number < 2^16
if 6.5536>=max(x_1)>0.65536
mult=10000;
else
end
if 0.65536>=max(x_1)>0.065536
mult=100000;
else
end
if 0.0065536>max(x_1)>=0.00065536
mult=1000000;
else
end
if 0.00065536>max(x_1)>=0.000065536
mult=10000000;
else
end
if 0.000065536>max(x_1)>=0.0000065536
mult=100000000;
else
end
multnum=mult*x_1 %multiplies string by multiplier found previously
roundnum=round(multnum) %rounds multnum to nearest whole number
b_16=dec2bin(roundnum,16) %converts roundnum to binary, 16-bits
|