A mathematical variable is a number that we don’t know yet and may have to solve for. For example, in the simple algebraic equation 2 + x = 3, the mathematical variable is x. A mathematical variable can also essentially be a placeholder for substituting a variety/range of numbers. For example, we can substitute any range of numbers into x in the function f(x) = x + 5 to find the value of a function, f(x). In a general sense, a variable varies its value. A variable’s value may be arbitrary, not specified, or even unknown.
In computer programming, such as MATLAB, a programming variable it is a connection between the variable’s name and a specific storage location in the computer’s memory. For example, the variable X references/points to its allocated storage, which contains some information about the variable like the value you assigned to it. Figure 1 shows a simple graphical illustration of this concept.
Although a variable in computer programming can be used as a mathematical variable, it can also be used for many more applications such as substitution, information storage, iteration, value comparison, and much more. Don’t worry if you are not sure how to do any of these in MATLAB, subsequent lessons will explain how to use variables for all of these things.
The following four variable naming rules are strictly enforced. If these four rules are not followed, MATLAB will throw errors and the program will not run. Note that the variable naming rules are the same for m-file naming rules (See Lesson XXXXX).
Given that you stay within naming rules, you are free to use any variable name you wish from “MATLAB’s” perspective. However, just because you can choose any variable name does not necessarily mean you should. Good variable names are essential to writing efficient and understandable code. Choosing your variable names wisely can have the following benefits:
This section contains a more explicit set of guidelines for naming your variables in MATLAB. These are strongly recommended; however, MATLAB will not throw any errors if these are not followed. Failing to follow good naming conventions, though, can make looking at a simple program seem intimidating and frustrating. Also note that different computer programming languages may have different naming conventions.
The following example shows a short MATLAB script with badly selected variable names. For this example do not worry about the function of this specific MATLAB m-file script. We have included examples and explanations for each guideline to elucidate each point more clearly and hopefully impress them on your memory.
% GUIDELINE 1 %Explanation: Looking at these calculations, it becomes hard to keep track % of what each variable means since the names are not descriptive. a = 2; z = 5/a; r = 8; q = z*r; % GUIDELINE 2 %Explanation: Although you do not know what for loops are (covered in % Chapter 6), this is one of the most common examples for a "short scope". % The variable name "iterationvar7" makes the code messy due to its % unnecessary length. for iterationvar7 = 1:5 iterationResult = iterationvar7*a + iterationvar7; end % GUIDELINE 3 %Explanation: This makes the program harder to read because there is no discernible marker between % words. pressuresensoroutput = 5.5; % GUIDELINE 4 %Explanation: It is unclear exactly what is true. Is it good or not? Is it % max or not? isNotGood = true; isNotMax = false; % GUIDELINE 5 %Explanation: Having very long variable names also makes the code difficult to read even if other % guidelines, like CamelCase, are followed. Additionally, variable names that are this % long are rarely necessary for description and can usually be shortened. voltageReadingFromSecondSensor = 10; MAXimumnumberofreadings = 20; %this violates both guidelines 3 and 5 and compounds the problem[Try this code yourself with Octave Online! Click Here]
In example 2, we rewrite our first MATLAB script example to put our guidelines into practice. Again, for this example do not worry about the function or meaning of this specific MATLAB m-file script. Note that the added white space and alignment further enhance readability.
%Note: We feel that the good variable names in this example should be obviously better and do not % require further explanations beyond what we have already given. % GUIDELINE 1 scale = 2; % GUIDELINE 2 for reading = 1:5 readingResult = reading*scale + reading end % GUIDELINE 3 pressureSensorOutput = 5.5; % GUIDELINE 4 isSensorGood = true; isVoltMax = false; % GUIDELINE 5 voltageReadingSensor2 = 10; %Note, this could be further abbreviated to the following depending on your preference voltReadSensor2 = 10;[Try this code yourself with Octave Online! Click Here]
In the next lesson, we will discover how to store text in a variable.