File: Script-Files.html

package info (click to toggle)
octave 10.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 145,388 kB
  • sloc: cpp: 335,976; ansic: 82,241; fortran: 20,963; objc: 9,402; sh: 8,756; yacc: 4,392; lex: 4,333; perl: 1,544; java: 1,366; awk: 1,259; makefile: 660; xml: 192
file content (171 lines) | stat: -rw-r--r-- 8,475 bytes parent folder | download
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
<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Script Files (GNU Octave (version 10.3.0))</title>

<meta name="description" content="Script Files (GNU Octave (version 10.3.0))">
<meta name="keywords" content="Script Files (GNU Octave (version 10.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="Functions-and-Scripts.html" rel="up" title="Functions and Scripts">
<link href="Function-Handles-and-Anonymous-Functions.html" rel="next" title="Function Handles and Anonymous Functions">
<link href="Function-Files.html" rel="prev" title="Function Files">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
strong.def-name {font-family: monospace; font-weight: bold; font-size: larger}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">


</head>

<body lang="en">
<div class="section-level-extent" id="Script-Files">
<div class="nav-panel">
<p>
Next: <a href="Function-Handles-and-Anonymous-Functions.html" accesskey="n" rel="next">Function Handles and Anonymous Functions</a>, Previous: <a href="Function-Files.html" accesskey="p" rel="prev">Function Files</a>, Up: <a href="Functions-and-Scripts.html" accesskey="u" rel="up">Functions and Scripts</a> &nbsp; [<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>
<h3 class="section" id="Script-Files-1"><span>11.11 Script Files<a class="copiable-link" href="#Script-Files-1"> &para;</a></span></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>
<p>Unlike a function file, a script file must <em class="emph">not</em> begin with the
keyword <code class="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>
<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>
<p>Even though a script file may not begin with the <code class="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 class="code">function</code>.  If you have no
other statements to evaluate, you can use a statement that has no
effect, like this:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted"># Prevent Octave from thinking that this
# is a function file:

1;

# Define function one:

function one ()
  ...
</pre></div></div>

<p>To have Octave read and compile these functions into an internal form,
you need to make sure that the file is in Octave&rsquo;s load path
(accessible through the <code class="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>
<p>If the first token in a file (ignoring comments) is <code class="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>
<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,
</p>
<div class="example">
<div class="group"><pre class="example-preformatted"># not a function file:
1;
function foo ()
  do_something ();
endfunction
function do_something ()
  do_something_else ();
endfunction
</pre></div></div>

<p>even though the function <code class="code">do_something</code> is not defined before it is
referenced in the function <code class="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>
<p>Since Octave doesn&rsquo;t look for definitions until they are needed, the
following code will always print &lsquo;<samp class="samp">bar = 3</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 class="file">bar.m</samp> in Octave&rsquo;s path.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">eval (&quot;bar = 3&quot;);
bar
</pre></div></div>

<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 class="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>
<p>Although Octave normally executes commands from script files that have
the name <samp class="file"><var class="var">file</var>.m</samp>, you can use the function <code class="code">source</code> to
execute commands from any file.
</p>
<a class="anchor" id="XREFsource"></a><span style="display:block; margin-top:-4.5ex;">&nbsp;</span>


<dl class="first-deftypefn">
<dt class="deftypefn" id="index-source"><span><strong class="def-name">source</strong> <code class="def-code-arguments">(<var class="var">file</var>)</code><a class="copiable-link" href="#index-source"> &para;</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-source-1"><span><strong class="def-name">source</strong> <code class="def-code-arguments">(<var class="var">file</var>, <var class="var">context</var>)</code><a class="copiable-link" href="#index-source-1"> &para;</a></span></dt>
<dd><p>Parse and execute the contents of <var class="var">file</var>.
</p>
<p>Without specifying <var class="var">context</var>, this is equivalent to executing commands
from a script file, but without requiring the file to be named
<samp class="file"><var class="var">file</var>.m</samp> or to be on the execution path.
</p>
<p>Instead of the current context, the script may be executed in either the
context of the function that called the present function
(<code class="code">&quot;caller&quot;</code>), or the top-level context (<code class="code">&quot;base&quot;</code>).
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="Calling-a-Function-by-its-Name.html#XREFrun">run</a>.
</p></dd></dl>



<ul class="mini-toc">
<li><a href="Publish-Octave-Script-Files.html" accesskey="1">Publish Octave Script Files</a></li>
<li><a href="Publishing-Markup.html" accesskey="2">Publishing Markup</a></li>
<li><a href="Jupyter-Notebooks.html" accesskey="3">Jupyter Notebooks</a></li>
</ul>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Function-Handles-and-Anonymous-Functions.html">Function Handles and Anonymous Functions</a>, Previous: <a href="Function-Files.html">Function Files</a>, Up: <a href="Functions-and-Scripts.html">Functions and Scripts</a> &nbsp; [<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>