MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Convert from Character Code


Character arrays and string arrays store each character as a 16-bit numeric value. Use
one of the integer conversion functions (e.g., uint8) or the double function to convert
characters to their numeric values, and char to revert to character representation:

name = 'Thomas R. Lee';

name = double(name)
name =
84 104 111 109 97 115 32 82 46 32 76 101 101

name = char(name)
name =

'Thomas R. Lee'

Convert Text that Represents Numeric Values


Use str2num to convert a character array to the numeric value it represents:

chr = '37.294e-1';

val = str2num(chr)
val =

3.7294

The str2double function converts a string array or a cell array of character vectors to
the double-precision values they represent:

c = {'37.294e-1'; '-58.375'; '13.796'};
str = string({'3.14159','2.718'});

d = str2double(c)

d =

3.7294
-58.3750
13.7960

x = str2double(str)

6 Characters and Strings

Free download pdf