<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Variables (GNU Octave (version 5.1.0))</title> <meta name="description" content="Variables (GNU Octave (version 5.1.0))"> <meta name="keywords" content="Variables (GNU Octave (version 5.1.0))"> <meta name="resource-type" content="document"> <meta name="distribution" content="global"> <meta name="Generator" content="makeinfo"> <link href="index.html#Top" rel="start" title="Top"> <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index"> <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> <link href="index.html#Top" rel="up" title="Top"> <link href="Global-Variables.html#Global-Variables" rel="next" title="Global Variables"> <link href="Comma-Separated-Lists-Generated-from-Structure-Arrays.html#Comma-Separated-Lists-Generated-from-Structure-Arrays" rel="prev" title="Comma Separated Lists Generated from Structure Arrays"> <style type="text/css"> <!-- a.summary-letter {text-decoration: none} blockquote.indentedblock {margin-right: 0em} blockquote.smallindentedblock {margin-right: 0em; font-size: smaller} blockquote.smallquotation {font-size: smaller} div.display {margin-left: 3.2em} div.example {margin-left: 3.2em} div.lisp {margin-left: 3.2em} div.smalldisplay {margin-left: 3.2em} div.smallexample {margin-left: 3.2em} div.smalllisp {margin-left: 3.2em} kbd {font-style: oblique} pre.display {font-family: inherit} pre.format {font-family: inherit} pre.menu-comment {font-family: serif} pre.menu-preformatted {font-family: serif} pre.smalldisplay {font-family: inherit; font-size: smaller} pre.smallexample {font-size: smaller} pre.smallformat {font-family: inherit; font-size: smaller} pre.smalllisp {font-size: smaller} span.nolinebreak {white-space: nowrap} span.roman {font-family: initial; font-weight: normal} span.sansserif {font-family: sans-serif; font-weight: normal} ul.no-bullet {list-style: none} --> </style> <link rel="stylesheet" type="text/css" href="octave.css"> </head> <body lang="en"> <a name="Variables"></a> <div class="header"> <p> Next: <a href="Expressions.html#Expressions" accesskey="n" rel="next">Expressions</a>, Previous: <a href="Data-Containers.html#Data-Containers" accesskey="p" rel="prev">Data Containers</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p> </div> <hr> <a name="Variables-1"></a> <h2 class="chapter">7 Variables</h2> <a name="index-variables_002c-user_002ddefined"></a> <a name="index-user_002ddefined-variables"></a> <p>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 </p> <div class="example"> <pre class="example">x x15 __foo_bar_baz__ fucnrdthsucngtagdjb </pre></div> <p>However, names like <code>__foo_bar_baz__</code> 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. </p> <p>Case is significant in variable names. The symbols <code>a</code> and <code>A</code> are distinct variables. </p> <p>A variable name is a valid expression by itself. It represents the variable’s current value. Variables are given new values with <em>assignment operators</em> and <em>increment operators</em>. See <a href="Assignment-Ops.html#Assignment-Ops">Assignment Expressions</a>. </p> <p>There is one built-in variable with a special meaning. The <code>ans</code> variable always contains the result of the last computation, where the output wasn’t assigned to any variable. The code <code>a = cos (pi)</code> will assign the value -1 to the variable <code>a</code>, but will not change the value of <code>ans</code>. However, the code <code>cos (pi)</code> will set the value of <code>ans</code> to -1. </p> <p>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. </p> <a name="index-ans"></a> <a name="XREFans"></a><dl> <dt><a name="index-ans-1"></a>Automatic Variable: <strong>ans</strong></dt> <dd><p>The most recently computed result that was not explicitly assigned to a variable. </p> <p>For example, after the expression </p> <div class="example"> <pre class="example">3^2 + 4^2 </pre></div> <p>is evaluated, the value returned by <code>ans</code> is 25. </p></dd></dl> <a name="XREFisvarname"></a><dl> <dt><a name="index-isvarname"></a><em></em> <strong>isvarname</strong> <em>(<var>name</var>)</em></dt> <dd><p>Return true if <var>name</var> is a valid variable name. </p> <p>A valid variable name is composed of letters, digits, and underscores ("_"), and the first character must not be a digit. </p> <p><strong>See also:</strong> <a href="Keywords.html#XREFiskeyword">iskeyword</a>, <a href="Status-of-Variables.html#XREFexist">exist</a>, <a href="Status-of-Variables.html#XREFwho">who</a>. </p></dd></dl> <a name="XREFmatlab_005flang_005fmakeValidName"></a><dl> <dt><a name="index-matlab_002elang_002emakeValidName"></a><em><var>varname</var> =</em> <strong>matlab.lang.makeValidName</strong> <em>(<var>str</var>)</em></dt> <dt><a name="index-matlab_002elang_002emakeValidName-1"></a><em><var>varname</var> =</em> <strong>matlab.lang.makeValidName</strong> <em>(…, <code>"ReplacementStyle"</code>, <var>rs</var>)</em></dt> <dt><a name="index-matlab_002elang_002emakeValidName-2"></a><em><var>varname</var> =</em> <strong>matlab.lang.makeValidName</strong> <em>(…, <code>"Prefix"</code>, <var>pfx</var>)</em></dt> <dt><a name="index-matlab_002elang_002emakeValidName-3"></a><em>[<var>varname</var>, <var>ismodified</var>] =</em> <strong>matlab.lang.makeValidName</strong> <em>(…)</em></dt> <dd> <p>Create valid variable name <var>varname</var> from <var>str</var>. </p> <p>The input <var>str</var> must be a string or a cell array of strings. The output <var>varname</var> will be of the same type. </p> <p>A valid variable name is a sequence of letters, digits, and underscores that does not begin with a digit. </p> <p>The <code>"ReplacementStyle"</code> option specifies how invalid characters are handled. Acceptable values are </p> <dl compact="compact"> <dt><code>"underscore"</code> (default)</dt> <dd><p>Replace all invalid characters with an underscore (<code>"_"</code>). </p> </dd> <dt><code>"delete"</code></dt> <dd><p>Remove any invalid character. </p> </dd> <dt><code>"hex"</code></dt> <dd><p>Replace all invalid characters with their hexadecimal representation. </p></dd> </dl> <p>Whitespace characters are always removed <strong>prior</strong> to the application of the <code>"ReplacementStyle"</code>. Lowercase letters following a whitespace will be changed to uppercase. </p> <p>The <code>"Prefix"</code> option specifies the string <var>pfx</var> to add as a prefix to the input if it begins with a digit. <var>pfx</var> must be a valid variable name itself. The default prefix is <code>"x"</code>. </p> <p>The optional output <var>ismodified</var> is a logical array indicating whether the respective element in <var>str</var> was a valid name or not. </p> <p><strong>See also:</strong> <a href="Keywords.html#XREFiskeyword">iskeyword</a>, <a href="#XREFisvarname">isvarname</a>, <a href="#XREFmatlab_005flang_005fmakeUniqueStrings">matlab.lang.makeUniqueStrings</a>. </p></dd></dl> <a name="XREFmatlab_005flang_005fmakeUniqueStrings"></a><dl> <dt><a name="index-matlab_002elang_002emakeUniqueStrings"></a><em><var>uniqstr</var> =</em> <strong>matlab.lang.makeUniqueStrings</strong> <em>(<var>str</var>)</em></dt> <dt><a name="index-matlab_002elang_002emakeUniqueStrings-1"></a><em><var>uniqstr</var> =</em> <strong>matlab.lang.makeUniqueStrings</strong> <em>(<var>str</var>, <var>ex</var>)</em></dt> <dt><a name="index-matlab_002elang_002emakeUniqueStrings-2"></a><em><var>uniqstr</var> =</em> <strong>matlab.lang.makeUniqueStrings</strong> <em>(<var>str</var>, <var>ex</var>, <var>maxlength</var>)</em></dt> <dt><a name="index-matlab_002elang_002emakeUniqueStrings-3"></a><em>[<var>uniqstr</var>, <var>ismodified</var>] =</em> <strong>matlab.lang.makeUniqueStrings</strong> <em>(…)</em></dt> <dd> <p>Construct a list of unique strings from a list of strings. </p> <p>The input <var>str</var> must be a string or a cell array of strings. The output <var>uniqstr</var> will be of the same type. </p> <p>The algorithm makes two strings unique by appending an underscore (<code>"_"</code> and a numeric count to the second string. </p> <p>If <var>ex</var> is a string or a cell array of strings, <var>uniqstr</var> will contain elements that are unique between themselves and with respect to <var>ex</var>. </p> <p>If <var>ex</var> is an index array or a logical array for <var>str</var> then it selects the subset of <var>str</var> that are made unique. Unselected elements are not modified. </p> <p>The optional input <var>maxlength</var> specifies the maximum length of any string in <var>uniqstr</var>. If an input string cannot be made unique without exceeding <var>maxlength</var> an error is emitted. </p> <p>The optional output <var>ismodified</var> is a logical array indicating whether each element in <var>str</var> was modified to make it unique. </p> <p><strong>See also:</strong> <a href="Sets.html#XREFunique">unique</a>, <a href="#XREFmatlab_005flang_005fmakeValidName">matlab.lang.makeValidName</a>. </p></dd></dl> <a name="XREFnamelengthmax"></a><dl> <dt><a name="index-namelengthmax"></a><em></em> <strong>namelengthmax</strong> <em>()</em></dt> <dd><p>Return the <small>MATLAB</small> compatible maximum variable name length. </p> <p>Octave is capable of storing strings up to <em>2^{31} - 1</em> in length. However for <small>MATLAB</small> compatibility all variable, function, and structure field names should be shorter than the length returned by <code>namelengthmax</code>. In particular, variables stored to a <small>MATLAB</small> file format (<samp>*.mat</samp>) will have their names truncated to this length. </p></dd></dl> <table class="menu" border="0" cellspacing="0"> <tr><td align="left" valign="top">• <a href="Global-Variables.html#Global-Variables" accesskey="1">Global Variables</a>:</td><td> </td><td align="left" valign="top"> </td></tr> <tr><td align="left" valign="top">• <a href="Persistent-Variables.html#Persistent-Variables" accesskey="2">Persistent Variables</a>:</td><td> </td><td align="left" valign="top"> </td></tr> <tr><td align="left" valign="top">• <a href="Status-of-Variables.html#Status-of-Variables" accesskey="3">Status of Variables</a>:</td><td> </td><td align="left" valign="top"> </td></tr> </table> <hr> <div class="header"> <p> Next: <a href="Expressions.html#Expressions" accesskey="n" rel="next">Expressions</a>, Previous: <a href="Data-Containers.html#Data-Containers" accesskey="p" rel="prev">Data Containers</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p> </div> </body> </html>