1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.8, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Variables (GNU Octave (version 7.3.0))</title>
<meta name="description" content="Variables (GNU Octave (version 7.3.0))">
<meta name="keywords" content="Variables (GNU Octave (version 7.3.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="index.html" rel="up" title="Top">
<link href="Expressions.html" rel="next" title="Expressions">
<link href="Data-Containers.html" rel="prev" title="Data Containers">
<style type="text/css">
<!--
a.copiable-anchor {visibility: hidden; text-decoration: none; line-height: 0em}
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
div.display {margin-left: 3.2em}
div.example {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}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
span:hover a.copiable-anchor {visibility: visible}
ul.no-bullet {list-style: none}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">
</head>
<body lang="en">
<div class="chapter" id="Variables">
<div class="header">
<p>
Next: <a href="Expressions.html" accesskey="n" rel="next">Expressions</a>, Previous: <a href="Data-Containers.html" accesskey="p" rel="prev">Data Containers</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<span id="Variables-1"></span><h2 class="chapter">7 Variables</h2>
<span id="index-variables_002c-user_002ddefined"></span>
<span id="index-user_002ddefined-variables"></span>
<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 Expressions</a>.
</p>
<p>There is one automatically created 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>
<span id="index-ans"></span>
<span id="XREFans"></span><dl class="def">
<dt id="index-ans-1"><span class="category">Automatic Variable: </span><span><strong>ans</strong><a href='#index-ans-1' class='copiable-anchor'> ¶</a></span></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>
<span id="XREFisvarname"></span><dl class="def">
<dt id="index-isvarname"><span class="category">: </span><span><em></em> <strong>isvarname</strong> <em>(<var>name</var>)</em><a href='#index-isvarname' class='copiable-anchor'> ¶</a></span></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>
<span id="XREFmatlab_005flang_005fmakeValidName"></span><dl class="def">
<dt id="index-matlab_002elang_002emakeValidName"><span class="category">: </span><span><em><var>varname</var> =</em> <strong>matlab.lang.makeValidName</strong> <em>(<var>str</var>)</em><a href='#index-matlab_002elang_002emakeValidName' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-matlab_002elang_002emakeValidName-1"><span class="category">: </span><span><em><var>varname</var> =</em> <strong>matlab.lang.makeValidName</strong> <em>(…, <code>"ReplacementStyle"</code>, <var>rs</var>)</em><a href='#index-matlab_002elang_002emakeValidName-1' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-matlab_002elang_002emakeValidName-2"><span class="category">: </span><span><em><var>varname</var> =</em> <strong>matlab.lang.makeValidName</strong> <em>(…, <code>"Prefix"</code>, <var>pfx</var>)</em><a href='#index-matlab_002elang_002emakeValidName-2' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-matlab_002elang_002emakeValidName-3"><span class="category">: </span><span><em>[<var>varname</var>, <var>ismodified</var>] =</em> <strong>matlab.lang.makeValidName</strong> <em>(…)</em><a href='#index-matlab_002elang_002emakeValidName-3' class='copiable-anchor'> ¶</a></span></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><span><code>"underscore"</code> (default)</span></dt>
<dd><p>Replace all invalid characters with an underscore (<code>"_"</code>).
</p>
</dd>
<dt><span><code>"delete"</code></span></dt>
<dd><p>Remove any invalid character.
</p>
</dd>
<dt><span><code>"hex"</code></span></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>
<span id="XREFmatlab_005flang_005fmakeUniqueStrings"></span><dl class="def">
<dt id="index-matlab_002elang_002emakeUniqueStrings"><span class="category">: </span><span><em><var>uniqstr</var> =</em> <strong>matlab.lang.makeUniqueStrings</strong> <em>(<var>str</var>)</em><a href='#index-matlab_002elang_002emakeUniqueStrings' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-matlab_002elang_002emakeUniqueStrings-1"><span class="category">: </span><span><em><var>uniqstr</var> =</em> <strong>matlab.lang.makeUniqueStrings</strong> <em>(<var>str</var>, <var>ex</var>)</em><a href='#index-matlab_002elang_002emakeUniqueStrings-1' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-matlab_002elang_002emakeUniqueStrings-2"><span class="category">: </span><span><em><var>uniqstr</var> =</em> <strong>matlab.lang.makeUniqueStrings</strong> <em>(<var>str</var>, <var>ex</var>, <var>maxlength</var>)</em><a href='#index-matlab_002elang_002emakeUniqueStrings-2' class='copiable-anchor'> ¶</a></span></dt>
<dt id="index-matlab_002elang_002emakeUniqueStrings-3"><span class="category">: </span><span><em>[<var>uniqstr</var>, <var>ismodified</var>] =</em> <strong>matlab.lang.makeUniqueStrings</strong> <em>(…)</em><a href='#index-matlab_002elang_002emakeUniqueStrings-3' class='copiable-anchor'> ¶</a></span></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>
<span id="XREFnamelengthmax"></span><dl class="def">
<dt id="index-namelengthmax"><span class="category">: </span><span><em></em> <strong>namelengthmax</strong> <em>()</em><a href='#index-namelengthmax' class='copiable-anchor'> ¶</a></span></dt>
<dd><p>Return the <small>MATLAB</small> compatible maximum variable name length.
</p>
<p>Octave is capable of storing strings up to <em class='math'>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>
<ul class="section-toc">
<li><a href="Global-Variables.html" accesskey="1">Global Variables</a></li>
<li><a href="Persistent-Variables.html" accesskey="2">Persistent Variables</a></li>
<li><a href="Status-of-Variables.html" accesskey="3">Status of Variables</a></li>
</ul>
</div>
<hr>
<div class="header">
<p>
Next: <a href="Expressions.html">Expressions</a>, Previous: <a href="Data-Containers.html">Data Containers</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|