Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > b38d2da330d1936e5ab1307c039c4941 > files > 533

octave-doc-3.6.4-3.mga4.noarch.rpm

<html lang="en">
<head>
<title>Variables - GNU Octave</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="GNU Octave">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="prev" href="Data-Containers.html#Data-Containers" title="Data Containers">
<link rel="next" href="Expressions.html#Expressions" title="Expressions">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
  pre.display { font-family:inherit }
  pre.format  { font-family:inherit }
  pre.smalldisplay { font-family:inherit; font-size:smaller }
  pre.smallformat  { font-family:inherit; font-size:smaller }
  pre.smallexample { font-size:smaller }
  pre.smalllisp    { font-size:smaller }
  span.sc    { font-variant:small-caps }
  span.roman { font-family:serif; font-weight:normal; } 
  span.sansserif { font-family:sans-serif; font-weight:normal; } 
--></style>
</head>
<body>
<div class="node">
<a name="Variables"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Expressions.html#Expressions">Expressions</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Data-Containers.html#Data-Containers">Data Containers</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="index.html#Top">Top</a>
<hr>
</div>

<h2 class="chapter">7 Variables</h2>

<p><a name="index-variables_002c-user_002ddefined-476"></a><a name="index-user_002ddefined-variables-477"></a>
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

<pre class="example">     x
     x15
     __foo_bar_baz__
     fucnrdthsucngtagdjb
</pre>
   <p class="noindent">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>Case is significant in variable names.  The symbols <code>a</code> and
<code>A</code> are distinct variables.

   <p>A variable name is a valid expression by itself.  It represents the
variable's current value.  Variables are given new values with
<dfn>assignment operators</dfn> and <dfn>increment operators</dfn>. 
See <a href="Assignment-Ops.html#Assignment-Ops">Assignment Expressions</a>.

   <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>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-g_t_0040code_007bans_007d-478"></a><!-- ans scripts/miscellaneous/ans.m -->
<a name="doc_002dans"></a>

<div class="defun">
&mdash; Automatic Variable: <b>ans</b><var><a name="index-ans-479"></a></var><br>
<blockquote><p>The most recently computed result that was not
explicitly assigned to a variable.  For example, after the expression

     <pre class="example">          3^2 + 4^2
</pre>
        <p class="noindent">is evaluated, the value returned by <code>ans</code> is 25. 
</p></blockquote></div>

<!-- isvarname src/utils.cc -->
   <p><a name="doc_002disvarname"></a>

<div class="defun">
&mdash; Built-in Function:  <b>isvarname</b> (<var>name</var>)<var><a name="index-isvarname-480"></a></var><br>
<blockquote><p>Return true if <var>name</var> is a valid variable name. 
<!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
<!-- A simple blank line produces the correct behavior. -->
<!-- @sp 1 -->

     <p class="noindent"><strong>See also:</strong> <a href="doc_002diskeyword.html#doc_002diskeyword">iskeyword</a>, <a href="doc_002dexist.html#doc_002dexist">exist</a>, <a href="doc_002dwho.html#doc_002dwho">who</a>. 
</p></blockquote></div>

<!-- genvarname scripts/general/genvarname.m -->
   <p><a name="doc_002dgenvarname"></a>

<div class="defun">
&mdash; Function File: <var>varname</var> = <b>genvarname</b> (<var>str</var>)<var><a name="index-genvarname-481"></a></var><br>
&mdash; Function File: <var>varname</var> = <b>genvarname</b> (<var>str, exclusions</var>)<var><a name="index-genvarname-482"></a></var><br>
<blockquote><p>Create unique variable(s) from <var>str</var>.  If <var>exclusions</var> is
given, then the variable(s) will be unique to each other and to
<var>exclusions</var> (<var>exclusions</var> may be either a string or a cellstr).

        <p>If <var>str</var> is a cellstr, then a unique variable is created for each
cell in <var>str</var>.

     <pre class="example">          x = 3.141;
          genvarname ("x", who ())
            &rArr; x1
</pre>
        <p>If <var>wanted</var> is a cell array, genvarname will make sure the returned
strings are distinct:

     <pre class="example">          genvarname ({"foo", "foo"})
            &rArr;
               {
                 [1,1] = foo
                 [1,2] = foo1
               }
</pre>
        <p>Note that the result is a char array/cell array of strings, not the
variables themselves.  To define a variable, <code>eval()</code> can be
used.  The following trivial example sets <code>x</code> to <code>42</code>.

     <pre class="example">          name = genvarname ("x");
          eval ([name " = 42"]);
            &rArr; x =  42
</pre>
        <p>Also, this can be useful for creating unique struct field names.

     <pre class="example">          x = struct ();
          for i = 1:3
            x.(genvarname ("a", fieldnames (x))) = i;
          endfor
            &rArr; x =
               {
                 a =  1
                 a1 =  2
                 a2 =  3
               }
</pre>
        <p>Since variable names may only contain letters, digits and underscores,
genvarname replaces any sequence of disallowed characters with
an underscore.  Also, variables may not begin with a digit; in this
case an underscore is added before the variable name.

        <p>Variable names beginning and ending with two underscores "__" are valid but
they are used internally by octave and should generally be avoided, therefore
genvarname will not generate such names.

        <p>genvarname will also make sure that returned names do not clash with
keywords such as "for" and "if".  A number will be appended if necessary. 
Note, however, that this does <strong>not</strong> include function names,
such as "sin".  Such names should be included in <var>avoid</var> if necessary. 
<!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
<!-- A simple blank line produces the correct behavior. -->
<!-- @sp 1 -->

     <p class="noindent"><strong>See also:</strong> <a href="doc_002disvarname.html#doc_002disvarname">isvarname</a>, <a href="doc_002dexist.html#doc_002dexist">exist</a>, <a href="doc_002dtmpnam.html#doc_002dtmpnam">tmpnam</a>, <a href="doc_002deval.html#doc_002deval">eval</a>. 
</p></blockquote></div>

<!-- namelengthmax scripts/miscellaneous/namelengthmax.m -->
   <p><a name="doc_002dnamelengthmax"></a>

<div class="defun">
&mdash; Function File:  <b>namelengthmax</b> ()<var><a name="index-namelengthmax-483"></a></var><br>
<blockquote><p>Return the <span class="sc">matlab</span> compatible maximum variable name length.  Octave is
capable of storing strings up to 2^31 - 1 in length. 
However for <span class="sc">matlab</span> compatibility all variable, function,
and structure field names should be shorter than the length supplied by
<code>namelengthmax</code>.  In particular variables stored to a <span class="sc">matlab</span> file
format will have their names truncated to this length. 
</p></blockquote></div>

<ul class="menu">
<li><a accesskey="1" href="Global-Variables.html#Global-Variables">Global Variables</a>
<li><a accesskey="2" href="Persistent-Variables.html#Persistent-Variables">Persistent Variables</a>
<li><a accesskey="3" href="Status-of-Variables.html#Status-of-Variables">Status of Variables</a>
</ul>

   </body></html>