Skip to main content
Engineering LibreTexts

7.5: Converting Between Strings and Character Vectors

  • Page ID
    136670
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \(\newcommand{\longvect}{\overrightarrow}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\ket}[1]{\left| #1 \right>}\)
    \(\newcommand{\bra}[1]{\left< #1 \right|}\)
    \(\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right| \left. #2 \vphantom{#1} \right>}\)
    \(\newcommand{\braopket}[3]{\left< #1 \vphantom{#2}\vphantom{#3} \right| #2 \vphantom{#1}\vphantom{#3} \left| #3 \vphantom{#1}\vphantom{#2} \right>}\)
    \(\newcommand{\qmvec}[1]{\mathbf{\vec{#1}}}\)
    \(\newcommand{\op}[1]{\hat{\mathbf{#1}}}\)
    \(\newcommand{\expect}[1]{\langle #1 \rangle}\)
    \(\newcommand{\dfn}[1]{\emph{\textbf{#1}}}\)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    The distinction between strings and character vectors is important because some MATLAB functions expect strings, while others expect character vectors. Also, strings and character vectors behave differently when they are placed into arrays, compared, concatenated, or converted.

    Converting a Character Vector to a String

    To convert a character vector to a string, use the string function.

    For example:

    myChar = 'Good Morning';
    myString = string(myChar)

    The result is a string scalar:

    myString = "Good Morning"

    This conversion is useful when you want to use newer MATLAB string functions or create string arrays.

    The string function can also convert numbers to strings:

    myStr = string(123)

    The result is:

    myStr = "123"

    Notice that "123" is no longer treated as the number 123. It is now text. This means that MATLAB will not treat it as a value that can be used directly in arithmetic unless it is converted back to a number.

    Converting a String to a Character Vector

    To convert a string to a character vector, use the char function.

    For example:

    myString = "The cat";
    myChar = char(myString)

    The result is:

    myChar = 'The cat'

    This can be useful when working with older MATLAB functions or examples that expect character vectors instead of strings.

    A Very Important Note About char with Numbers

    The char function can convert strings to character vectors, but when it receives a number, it does something different.

    For example, students might expect this command:

    char(123)

    to return:

    '123'

    But that is not what happens.

    Instead, MATLAB interprets the number 123 as a character code and returns the character associated with that code, which is:

    '{'

    This happens because MATLAB uses character encoding. In older basic text encoding systems such as ASCII, each character is represented internally by a number. In that table, the number 123 corresponds to the open curly brace character {.

    So, char(123) does not mean “turn the number 123 into the text 123.” It means “find the character whose code is 123.”

    This is a common beginner mistake.

    If your goal is to convert the number 123 into the text '123', use num2str or string, not char.

    For example:

    num2str(123)

    returns:

    '123'
    

    What Is ASCII?

    ASCII stands for American Standard Code for Information Interchange. It is one of the older and most widely recognized systems for representing text characters using numbers.

    Computers store everything as numbers. This includes letters, punctuation marks, spaces, and special symbols. ASCII provides a standard table that connects characters to numerical codes.

    For example, in the ASCII table:

    Character ASCII Code
    'A' 65
    'B' 66
    'C' 67
    'a' 97
    'b' 98
    'c' 99
    '0' 48
    '1' 49
    '2' 50
    space 32
    '{' 123

    This means the character 'A' is stored using the number 65, while the character 'a' is stored using the number 97. This also explains why uppercase and lowercase letters are not the same to a computer.

    For example:

    'A' == 'a'

    returns:

    0

    because uppercase 'A' and lowercase 'a' have different character codes.

    Finding the ASCII Code Associated Character

    The function double can be used to see the numerical code associated with a character.

    For example:

    double('A')

    returns:

    65

    and:

    double('a')

    returns:

    97

    If we apply double to a character vector, MATLAB returns the code for each character.

    For example:

    double('cat')

    returns:

    99 97 116

    because:

    'c' has code 99
    'a' has code 97
    't' has code 116

    This helps explain why character vectors can behave like arrays. A character vector is really an array of characters, and each character has a numerical code behind it.

    Converting Text Back to Numbers

    Sometimes we have a number stored as text and we need to convert it back to a numerical value.

    For example:

    strNum = "45.7";

    This looks like a number, but it is stored as text. To use it in a calculation, we can convert it using str2double.

    numValue = str2double(strNum)

    The result is:

    numValue = 45.7000

    Now MATLAB can use it in arithmetic.

    For example:

    numValue + 10

    returns:

    55.7000

    The str2double function also works with character vectors:

    str2double('3.14159')

    returns:

    3.1416

    This is useful when reading numerical values from user input, text files, or spreadsheet data where numbers may initially be stored as text.

    Summary of Common Conversion Functions

    Function Purpose Example
    string(x) Converts input to a string string(123) returns "123"
    char(x) Converts a string to a character vector, or converts a numeric code to a character char("cat") returns 'cat'; char(65) returns 'A'
    num2str(x) Converts a number to a character vector num2str(123) returns '123'
    str2double(x) Converts text to a double value str2double("123") returns 123
    double(x) Converts characters to their numeric character codes double('A') returns 65

    This page titled 7.5: Converting Between Strings and Character Vectors was last modified on Tue, 30 Jun 2026 02:11:53 GMT and is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Zara Noroozi.

    • Was this article helpful?