Exercises
Please complete each exercise or answer the question before reviewing the posted solution comments.
Use the clear
command before starting your work on a new problem
to ensure that any existing variable name assignments don't interfere with your work on
the new problem.
-
Is
1stList
a valid name for a variable in Matlab?No, variable names may not start with a digit. Though, they can contain digits after the first character.
-
Is
Bar Height
a valid name for a variable in Matlab?No, variable names may not blank spaces. Matlab uses blank spaces to recognize when one token, variable or operator, ends and the next begins. Because of this, blank spaces can not be used as part of a single variable name.
-
Is
Bar_Height
a valid name for a variable in Matlab?Yes, variable names may contain the underscore character (
_
). Matlab programmers often use the underscore character to separate multiple words within a variable name. -
How many variables are created by the following Matlab statements and what are their values?
clear ; myHeight = 72 ; myheight = 71 ; myHeight = 70 ; myHEIGHT = 69 ; MyHeight = 68 ; MYheight = 67 ; myHeighT = 66 ; myHeight = 65 ;
Some answer choices...
- 0 variables since the output was suppressed on all statements
- 1 variable with the last value 65.
- 4 variables with the values 65, 67, 68, 69, and 71.
- 6 variables with the values 65, 66, 67, 68, 69, and 71.
- 8 variables, with each value listed above
6 variables with the values 65, 66, 67, 68, 69, and 71.
-
Is
priceIn$
a valid name for a variable in Matlab?No, variable names may not contain characters other than letters, digits, and the underscore character.
-
Is
Minutes&Seconds
a valid name for a variable in Matlab?No, variable names may not contain characters other than letters, digits, and the underscore character. Besides, two values should be stored in two different variables.
-
What is the difference between including a semi-colon at the end of the command or not including the semi-colon?
Including a semi-colon suppresses the output of a command. The command still executes as expected, but there is no output displayed in the command window.
-
Open or create an m-file that contains the following text:
function sa = surfaceArea() % Returns the surface area of a cube with sides that are "sideLength" long. sa = 6 * sideLength.^2 ;
Add code that tells Matlab that the variable
sideLength
is a global variable.