Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > 3e60ff9d4d6f58c8fbd17208f14089fa > files > 360

octave-doc-3.2.3-3mdv2010.0.i586.rpm

<html lang="en">
<head>
<title>Script Files - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Functions-and-Scripts.html#Functions-and-Scripts" title="Functions and Scripts">
<link rel="prev" href="Function-Files.html#Function-Files" title="Function Files">
<link rel="next" href="Function-Handles-Inline-Functions-and-Anonymous-Functions.html#Function-Handles-Inline-Functions-and-Anonymous-Functions" title="Function Handles Inline Functions and Anonymous Functions">
<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="Script-Files"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Function-Handles-Inline-Functions-and-Anonymous-Functions.html#Function-Handles-Inline-Functions-and-Anonymous-Functions">Function Handles Inline Functions and Anonymous Functions</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Function-Files.html#Function-Files">Function Files</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Functions-and-Scripts.html#Functions-and-Scripts">Functions and Scripts</a>
<hr>
</div>

<h3 class="section">11.8 Script Files</h3>

<p>A script file is a file containing (almost) any sequence of Octave
commands.  It is read and evaluated just as if you had typed each
command at the Octave prompt, and provides a convenient way to perform a
sequence of commands that do not logically belong inside a function.

   <p>Unlike a function file, a script file must <em>not</em> begin with the
keyword <code>function</code>.  If it does, Octave will assume that it is a
function file, and that it defines a single function that should be
evaluated as soon as it is defined.

   <p>A script file also differs from a function file in that the variables
named in a script file are not local variables, but are in the same
scope as the other variables that are visible on the command line.

   <p>Even though a script file may not begin with the <code>function</code>
keyword, it is possible to define more than one function in a single
script file and load (but not execute) all of them at once.  To do
this, the first token in the file (ignoring comments and other white
space) must be something other than <code>function</code>.  If you have no
other statements to evaluate, you can use a statement that has no
effect, like this:

<pre class="example">     # Prevent Octave from thinking that this
     # is a function file:
     
     1;
     
     # Define function one:
     
     function one ()
       ...
</pre>
   <p>To have Octave read and compile these functions into an internal form,
you need to make sure that the file is in Octave's load path
(accessible through the <code>path</code> function), then simply type the
base name of the file that contains the commands.  (Octave uses the
same rules to search for script files as it does to search for
function files.)

   <p>If the first token in a file (ignoring comments) is <code>function</code>,
Octave will compile the function and try to execute it, printing a
message warning about any non-whitespace characters that appear after
the function definition.

   <p>Note that Octave does not try to look up the definition of any identifier
until it needs to evaluate it.  This means that Octave will compile the
following statements if they appear in a script file, or are typed at
the command line,

<pre class="example">     # not a function file:
     1;
     function foo ()
       do_something ();
     endfunction
     function do_something ()
       do_something_else ();
     endfunction
</pre>
   <p class="noindent">even though the function <code>do_something</code> is not defined before it is
referenced in the function <code>foo</code>.  This is not an error because
Octave does not need to resolve all symbols that are referenced by a
function until the function is actually evaluated.

   <p>Since Octave doesn't look for definitions until they are needed, the
following code will always print &lsquo;<samp><span class="samp">bar = 3</span></samp>&rsquo; whether it is typed
directly on the command line, read from a script file, or is part of a
function body, even if there is a function or script file called
<samp><span class="file">bar.m</span></samp> in Octave's path.

<pre class="example">     eval ("bar = 3");
     bar
</pre>
   <p>Code like this appearing within a function body could fool Octave if
definitions were resolved as the function was being compiled.  It would
be virtually impossible to make Octave clever enough to evaluate this
code in a consistent fashion.  The parser would have to be able to
perform the call to <code>eval</code> at compile time, and that would be
impossible unless all the references in the string to be evaluated could
also be resolved, and requiring that would be too restrictive (the
string might come from user input, or depend on things that are not
known until the function is evaluated).

   <p>Although Octave normally executes commands from script files that have
the name <samp><var>file</var><span class="file">.m</span></samp>, you can use the function <code>source</code> to
execute commands from any file.

<!-- parse.cc -->
   <p><a name="doc_002dsource"></a>

<div class="defun">
&mdash; Built-in Function:  <b>source</b> (<var>file</var>)<var><a name="index-source-642"></a></var><br>
<blockquote><p>Parse and execute the contents of <var>file</var>.  This is equivalent to
executing commands from a script file, but without requiring the file to
be named <samp><var>file</var><span class="file">.m</span></samp>. 
</p></blockquote></div>

   </body></html>