Skip to main content
Engineering LibreTexts

8.5: Variables

  • Page ID
  • \( \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}}\)

    Any result you wish to keep for a while may be assigned to a variable other thanans:

    ≫ x = pi/7
    ≫ cos(x)
    ≫ y = sin(x)^2+cos(x)^2;
    ≫ y

    A semicolon (;) at the end of the line suppresses printing of the result, as when we calculated \(y\) in the next-to-last line just shown. This feature is especially useful when writing MATLAB programs where intermediate results are not of interest and when working with large matrices.

    MATLAB supports the dynamic creation of variables. You can create your own variables by just assigning a value to a variable. For example, typex=3.5+4.2. Then the real variablexcontains the value7.7. Variable names must start with an alphabetical character and be less than nineteen characters long. If you typex=-3*4.0, the content7.7 is replaced by the value-12. Some commands allow you to keep track of all the variables that you have already created in your session. Typewhoorwhos to get the list and names of the variables currently in memory (whosgives more information thanwho). To clear all the variables, type in clear. To clear a single variable (or several) from the list, follow the commandclear by the name of the variable you want to delete or by a list of variable names separated by spaces. Try it now.

    MATLAB is case sensitive. In other words, \(x\) and \(X\) are two different variables. You can control the case sensitivity of MATLAB by entering the commandcasesen, which toggles the sensitivity. The commandcasesenon enforces case sensitivity, andcasesenoffcancels it.

    If one line is not enough to enter your command, then finish the first line with two dots (. . ) and continue on the next line. You can enter more than one command per line by separating them with commas if you want the result displayed or with semicolons if you do not want the result displayed. For example, type

    ≫ theta = pi/7; x = cos(theta); y = sin(theta);
    ≫ x,y

    to first computetheta,cos(theta), andsin(theta)and then to print \(x\) and \(y\).

    This page titled 8.5: Variables is shared under a CC BY 3.0 license and was authored, remixed, and/or curated by Louis Scharf (OpenStax CNX) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?