function b=num2bin(n) %NUM2BIN Convert floating point number to binary string. % NUM2BIN(N) returns the binary representation of the floating point % number N in the form [sign][exponent][mantissa]. % % Example % float2bin(single(9.4)) returns % [0][10000000010][0010110011001100110011000000000000000000000000000000] % float2bin(double(9.4)) returns % [0][10000000010][0010110011001100110011001100110011001100110011001101] % % See also DEC2BIN, BIN2DEC, HEX2DEC, BASE2DEC. % Francisco J. Beron-Vera % $Revision: 1.2 $ $Date: 2006/08/30 16:13:31 $ % Convert to hex string. h=sprintf('%bx',n); % Convert to cell array of strings. h=cellstr(h'); % Convert hex numerals to decimal numbers. h=hex2dec(h); % Convert numeric array to array of binary strings. b=dec2bin(h); % Pad with zeros if necessary. b=[repmat(num2str(zeros(16,1)),[1 4-size(b,2)]) b]; % Concatenate binary strings. b=b'; b=b(:)'; % Output as a string of the form [sign][exponent][mantissa]. b=['[' b(1) '][' b(2:12) '][' b(13:end) ']'];