7.11: Converting and Formatting Text
- Page ID
- 136676
\( \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}}}\)
Many programs need to move between text and numbers. For example, input from a user may arrive as text, but you may need to convert it to a number before doing a calculation. Or you may calculate a number and then convert it to text so it can be included in a message.
10.1 Changing Case with upper and lower
|
Example 24: Convert letters to lowercase or uppercase mystr = "abCDEfgh"; allLow = lower(mystr) allUp = upper(mystr) |
These functions are especially useful when comparing user input. If you convert both the user input and the target text to the same case, the comparison becomes easier.
10.2 Formatting Text with sprintf
The function sprintf works similarly to fprintf, but instead of printing the formatted text to the Command Window, it stores the formatted text in a variable.
|
Example 25: Format numbers into text A = pi*ones(1,3); txt = sprintf('%f | %.2f | %12f', A) |
This is useful when you want to build a message that includes numeric results.
|
Example 26: Create a formatted message radius = 3; area = pi*radius^2; message = sprintf("The area is %.2f square units.", area) |
10.3 Converting Numbers and Matrices to Text
|
Example 27: Convert numbers and matrices to character vectors myMat = [1 2; 3 4]; mat2str(myMat) num2str(pi) num2str(pi, 2) num2str(pi, 4) |
The function mat2str converts a matrix into a character vector. The function num2str converts a number into a character vector, and you can control the number of significant digits.
10.4 Converting Text to Numbers
|
Example 28: Convert text to numeric values str2double('3.14159') str2double(["2.71" "3.1415"]) str2num('1234') S = ['1 2'; '3 4']; str2num(S) |
For many beginner programs, str2double is a good choice when converting a single number written as text into a numeric value. The function str2num can interpret a character vector as a numeric expression, but it should be used more carefully.
|
Common Beginner Pattern Ask the user for text using input(prompt, s), then use str2double if the text should become a number. |
10.5 Reading Text from User Input
When you expect the user to type text, use input with the second argument s.
|
Example 29: Read text from the user str = input('Please enter your first name: ', 's'); |
The s tells MATLAB to store the user input as text rather than trying to evaluate it as a MATLAB expression.


