%% Convert Numeric Values to Text
% This example shows how to convert numeric values to text and append them to larger pieces of text. For example, you might want to add a label or title to a plot, where the label includes a number that describes a characteristic of the plot.
% You can convert numeric values to strings or character vectors. You can convert them to decimal, hexadecimal, or binary text representations. The same techniques also convert datetime or duration values to text.
%% Numeric Arrays
% To convert a number to a string that represents it, use the string function.

A = 3.1416
str = string(A)

%
% The string function converts a numeric array to a string array having the same size.

A = [256 3.1416 8.9e-3]
str = string(A)

%
% While the char function also converts numbers to characters, it does not perform the same conversion as string. Rather, it converts numbers to individual characters (for example, converting 65 to the letter A). It is not recommended for converting floating-point numbers to strings that represent them.
% You can specify the format of the output text using the compose, sprintf, or num2str functions. These functions accept format specifiers so that you can specify precision, field width, and exponential notation.

str = compose("%9.7f",pi)

%
% If the input is a numeric array, then compose returns a string array. Return a string array that represents numbers using exponential notation.

A = [256 3.1416 8.9e-3];
str = compose("%5.2e",A)

%
% The compose, sprintf, and num2str functions return different types of output.
% compose returns a string array that has the same size as the input numeric array.
% sprintf returns a character vector or string scalar, even when the input is a numeric array.
% num2str always returns a character vector.
% The best practice is to use either the string or compose function to convert values.
% Use string to convert numeric values.
% Use compose to also control the format of output text.
% Avoid the char function. It treats numbers as integers representing Unicode values and converts them to the corresponding characters.
%
%% Append Converted Numeric Values to Text
% A typical use for these functions is to convert numbers to text so that you can append them to other pieces of text, such as file names or labels on a plot.
% For example, plot a sine wave and add a title to the plot. Calculate the frequency of the wave and add a string representing that value in the title of the plot.

X = linspace(0,2*pi);
Y = sin(X);
plot(X,Y)
xlabel("X")
ylabel("Y")
freq = string(1/(2*pi));
titleStr = append("Sine Wave, Frequency = ",freq," Hz")
title(titleStr)

%
% As an alternative, you can use the plus operator to add text to the end of a string. However, plus treats character vectors like numeric arrays.

titleStr = "Sine Wave, Frequency = " + freq + " Hz"

% The append function appends text to string arrays, character vectors, and cell arrays of character vectors. For this reason, the best practice is to use the append function.
%
%% Hexadecimal and Binary Values
% You can represent hexadecimal and binary values in your code either using text or using literals. The recommended way to represent them is to write them as literals. You can write hexadecimal and binary literals using the 0x and 0b prefixes respectively. However, it can sometimes be useful to represent such values as text, using the dec2hex or dec2bin functions.
% For example, set a bit in a binary value. If you specify the binary value using a literal, then it is stored as an integer. After setting one of the bits, display the new binary value as text using the dec2bin function.

register = 0b10010110
register = bitset(register,5,0)
binStr = dec2bin(register)

%
%% Dates and Times
% MATLAB provides the datetime and duration data types to store dates and times, and to treat them as numeric values. To convert such values to text representing dates and times, use the string and char functions.
% Represent the current date and time as a string.

d = datetime('now')
str = string(d)

%
% Represent an array of duration values.

D = seconds(1:5)
str = string(D)

% Copyright 2019 The MathWorks, Inc.