Skip to main content
Library homepage
 
Engineering LibreTexts

1.3.1: More About Valid Variable Names

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

      Variable Name Rules

      MATLAB variable names must start with a letter. The rest of the name can be letters, numbers ot the underscore symbol. The letters can be upper or lower case. These are valid variable names:

      b, B, Basketball, Basket_Ball, basket5ball, basketBall_12345

      These are not valid names:

      Basket-Ball, Basket_Ball!, Basket?Ball, Basket Ball.

      Descriptive names are encouraged, in order to make the code readable. So 'pressure' and 'press' are better names than just 'p'. Names are allowed to be very long, but they should not be more than a quater of the command window, so keep them to less than 20 letters, unless there is a very good reason to make them longer.

      Sometimes, you may have 2 version of the same quantity, such as a distance in both inches and centimeters. A good practice is to give them related names with identifiers, such as dist_inch and dist_cm, so that you always know which units you are using.

      Variable Names to Avoid

      Don't use the name of a function. For example, don't use these names:

      sqrt, sin, cos, log10, sum.

      Also, don't use 2 names that only differ for upper vs. lower case letters; that can lead to confusion. For example, having 2 variables named kg and Kg should be avoided.

      Exercise \(\PageIndex{1}\) Good Variable Names

      Which of the following are good variable names? Make a decision about each before looking at the answers.

      You may enter each in the command window by setting the name = 1, in order to see if it is valid.

      You may use the exist() function to see if it is a function name.

      sind

      sinx

      A-very_long_name

      Even_123_longer456_name_9876543210

      important!

      machuPichu

      _next

      1one1

      Answer

      sind -- a function name--don't use it

      sinx -- valid

      A-very_long_name -- not valid becuz of the -, which is interpreted as a minus sign

      Even_123_longer456_name_9876543210 -- valid

      important! -- nor valid becuz of the !

      machuPichu

      _next -- not valid. It can't start with an underscore

      1one1 -- not valid. It can't start with a number

      .

      MATLAB keywords

      It is not allowed to use MATLAB keywords. These are words that MATLAB reserves for program structure. This is the list of keywords:

      'break'
      'case'
      'catch'
      'classdef'
      'continue'
      'else'
      'elseif'
      'end'
      'for'
      'function'
      'global'
      'if'
      'otherwise'
      'parfor'
      'persistent'
      'return'
      'spmd'
      'switch'
      'try'
      'while'


      This page titled 1.3.1: More About Valid Variable Names 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?