Skip to main content
Engineering LibreTexts

4.7: disp() and num2str() Functions and Concatenation

  • Page ID
    84416
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\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}}\)

    By Carey Smith.

    disp()

    The disp() function can display a number variable or a character string.

    Example \(\PageIndex{1}\) disp() a character string or a number

    % Display a character string:
    disp('theta')

    The output is:

    theta

    % Display a number variable
    theta = 2*pi;
    disp(theta)

    The output is:

    6.2832

    Solution

    Add example text here.

    To display a message that has a quote mark, you need to enter 2 quotes marks, like this:
    disp('Lincoln''s birthday is Feb. 12')

    Concatenation

    Definition: Concatenation

    Concatenation: Connect 2 or more vectors or character strings into a single vector or character string.

    Example \(\PageIndex{2}\) Concatenation of 2 vectors

    a = 1:4
    b = [12, 13, 14]
    % Put 2 or more items of the same type within square brackets to concatenate them:
    ab = [a, b]

    Result:
    [1 2 3 4 12 13 14]

    Solution

    Add example text here.

    .

    Example \(\PageIndex{3}\) Concatenation of 2 character strings

    Concatenation of 2 character strings:
    name1 = 'Frederick'
    name2 = 'Douglas'
    fullname = [name1,' ',name2]

    Result:

    'Frederick Douglas'
     

    % The ' ' puts a space between the 2 names

    Solution

    Add example text here.

    num2str()

    In order to concatenate a character string with a number, the number needs to be converted to a character string. That can be done with the num2str() function. num2str(x) converts the number variable, x, to a character string.

    Example \(\PageIndex{4}\)

    Examples:

    x = 1/7

    num2str(x) % This displays x with the default number of significant figures
    ans =
        '0.14286'
    num2str(x, 3) % This displays x with 3 significant figures

    ans =
        '0.14286'

     

    Solution

    Add example text here.

    Each digit in the number is converted tp a character using the ASCII standard. For details of the the ASCII standard, see https://en.Wikipedia.org/wiki/ASCII.

    Example \(\PageIndex{5}\)Concatenation of a character string with a number

    Display the variable name 'theta' and its value (a number)

    theta = 2*pi;

    The 2 items need to be of the same type (format).

    Theta's type is a double-precision number.

    Character strings (text) are stored in ASCII format.

    You do not need to know any details about ASCII format, except that it is different than number format. If you want to learn more, see:

    https://en.wikipedia.org/wiki/ASCII

    We convert the number to a character string using the num2str() function as follows:

    theta_char_str = num2str(theta)

    Result: '6.2832'

    The next line of code concatenates the variable name and its value into a single character string:

    theta_msg = ['theta = ', theta_char_str]

    Result: 'theta = 6.2832'

    We can combine displaying the name with the conversion of the number to a string in 1 line of code as shown here:
    disp(['theta = ', num2str(theta)])

    Same result: 'theta = 6.2832'

    Solution

    Add example text here.

    .

    Exercise \(\PageIndex{1}\) Help on num2str

    Part a: For this exercise you will use num2str() to disply pi with 3 significant figures and with with 10 significant figures. To do this, you need to read the help on the num2str() function. This also gives you practice using the help.

    Method 1: In the command window, type:

    help num2str

    This works in either MATLAB or Octave.

    Method 2 (MATLAB): enter "num2str" in either the editor or the command window, then right-click on num2str and select "Help on Selection". A window pops up that lets you open MATLAB's help browser with documentation and examples.

    Method 2 (Octave): Under the command window click "Documentation". At the top, select the "Function Index" tab and type num2str in the search box.

    Part b: Create an m-file. In this file, use num2str to display pi with 3 significant figures.

    Part c: Also use num2str to display pi with 10 significant figures.

    Save your file and submit it.

    Answer

    Add texts here. Do not delete this text first.

    .


    This page titled 4.7: disp() and num2str() Functions and Concatenation is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith.

    • Was this article helpful?