Programming and Data Types

Numeric/String Conversion

The MATLAB string/numeric conversion functions change numeric values into character strings. You can store numeric values as digit-by-digit string representations, or convert a value into a hexadecimal or binary string. Consider a the scalar

• x = 5317;

By default, MATLAB stores the number x as a 1-by-1 double array containing the value 5317. The int2str (integer to string) function breaks this scalar into a 1-by-4 vector containing the string '5317'.

• y = int2str(x);
size(y)

ans =

1     4

A related function, num2str, provides more control over the format of the output string. An optional second argument sets the number of digits in the output string, or specifies an actual format.

• p = num2str(pi,9)

p =

3.14159265

Both int2str and num2str are handy for labeling plots. For example, the following lines use num2str to prepare automated labels for the x-axis of a plot.

• function plotlabel(x,y)
plot(x,y)
str1 = num2str(min(x));
str2 = num2str(max(x));
out = ['Value of f from ' str1 ' to ' str2];
xlabel(out);

Another class of numeric/string conversion functions changes numeric values into strings representing a decimal value in another base, such as binary or hexadecimal representation. For example, the dec2hex function converts a decimal value into the corresponding hexadecimal string.

• dec_num = 4035;

hex_num = dec2hex(dec_num)
hex_num =
FC3

See the strfun directory for a complete listing of string conversion functions.

 Searching with Tokens Array/String Conversion