1.6.1: More About Valid Variable Names

Matlab and Octave Programming for STEM Applications · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under CC BY-NC-SA 4.0, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    1.6.1: More About Valid Variable Names

      Variable Name Rules

      MATLAB variable names must start with a letter. The rest of the name can be letters, numbers or 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 quarter of the command window, so keep them to less than 20 letters, unless there is a very good reason to make them longer. (The maximumn allowed name length is 63 characters.)

      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.6.1: More About Valid Variable Names was last modified on Sun, 25 Aug 2024 19:45:43 GMT and is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith.