Next: , Previous:   [Contents][Index]


7 Variables

Variables let you give names to values and refer to them later. You have already seen variables in many of the examples. The name of a variable must be a sequence of letters, digits and underscores, but it may not begin with a digit. Octave does not enforce a limit on the length of variable names, but it is seldom useful to have variables with names longer than about 30 characters. The following are all valid variable names

x
x15
__foo_bar_baz__
fucnrdthsucngtagdjb

However, names like __foo_bar_baz__ that begin and end with two underscores are understood to be reserved for internal use by Octave. You should not use them in code you write, except to access Octave’s documented internal variables and built-in symbolic constants.

Case is significant in variable names. The symbols a and A are distinct variables.

A variable name is a valid expression by itself. It represents the variable’s current value. Variables are given new values with assignment operators and increment operators. See Assignment Expressions.

There is one automatically created variable with a special meaning. The ans variable always contains the result of the last computation, where the output wasn’t assigned to any variable. The code a = cos (pi) will assign the value -1 to the variable a, but will not change the value of ans. However, the code cos (pi) will set the value of ans to -1.

Variables in Octave do not have fixed types, so it is possible to first store a numeric value in a variable and then to later use the same name to hold a string value in the same program. Variables may not be used before they have been given a value. Doing so results in an error.

Automatic Variable: ans

The most recently computed result that was not explicitly assigned to a variable.

For example, after the expression

3^2 + 4^2

is evaluated, the value returned by ans is 25.

: tf = isvarname (name)

Return true if name is a valid variable name.

A valid variable name is composed of letters, digits, and underscores ("_"), and the first character must not be a digit.

See also: iskeyword, exist, who.

: varname = matlab.lang.makeValidName (str)
: varname = matlab.lang.makeValidName (…, "ReplacementStyle", rs)
: varname = matlab.lang.makeValidName (…, "Prefix", pfx)
: [varname, ismodified] = matlab.lang.makeValidName (…)

Create valid variable name varname from str.

The input str must be a string or a cell array of strings. The output varname will be of the same type.

A valid variable name is a sequence of letters, digits, and underscores that does not begin with a digit.

The "ReplacementStyle" option specifies how invalid characters are handled. Acceptable values are

"underscore" (default)

Replace all invalid characters with an underscore ("_").

"delete"

Remove any invalid character.

"hex"

Replace all invalid characters with their hexadecimal representation.

Whitespace characters are always removed prior to the application of the "ReplacementStyle". Lowercase letters following a whitespace will be changed to uppercase.

The "Prefix" option specifies the string pfx to add as a prefix to the input if it begins with a digit. pfx must be a valid variable name itself. The default prefix is "x".

The optional output ismodified is a logical array indicating whether the respective element in str was a valid name or not.

See also: iskeyword, isvarname, matlab.lang.makeUniqueStrings.

: uniqstr = matlab.lang.makeUniqueStrings (str)
: uniqstr = matlab.lang.makeUniqueStrings (str, ex)
: uniqstr = matlab.lang.makeUniqueStrings (str, ex, maxlength)
: [uniqstr, ismodified] = matlab.lang.makeUniqueStrings (…)

Construct a list of unique strings from a list of strings.

The input str must be a string or a cell array of strings. The output uniqstr will be of the same type.

The algorithm makes two strings unique by appending an underscore ("_" and a numeric count to the second string.

If ex is a string or a cell array of strings, uniqstr will contain elements that are unique between themselves and with respect to ex.

If ex is an index array or a logical array for str then it selects the subset of str that are made unique. Unselected elements are not modified.

The optional input maxlength specifies the maximum length of any string in uniqstr. If an input string cannot be made unique without exceeding maxlength an error is emitted.

The optional output ismodified is a logical array indicating whether each element in str was modified to make it unique.

See also: unique, matlab.lang.makeValidName.

: n = namelengthmax ()

Return the MATLAB compatible maximum variable name length.

Octave is capable of storing strings up to 2^{31} - 1 in length. However for MATLAB compatibility all variable, function, and structure field names should be shorter than the length returned by namelengthmax. In particular, variables stored to a MATLAB file format (*.mat) will have their names truncated to this length.


Next: Expressions, Previous: Data Containers   [Contents][Index]