Sophie

Sophie

distrib > Mageia > 7 > i586 > by-pkgid > 641ebb3060c35990cc021d8f7aaf9aca > files > 225

octave-doc-5.1.0-7.1.mga7.noarch.rpm

<!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>Evaluation in a Different Context (GNU Octave (version 5.1.0))</title>

<meta name="description" content="Evaluation in a Different Context (GNU Octave (version 5.1.0))">
<meta name="keywords" content="Evaluation in a Different Context (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="Evaluation.html#Evaluation" rel="up" title="Evaluation">
<link href="Statements.html#Statements" rel="next" title="Statements">
<link href="Calling-a-Function-by-its-Name.html#Calling-a-Function-by-its-Name" rel="prev" title="Calling a Function by its Name">
<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="Evaluation-in-a-Different-Context"></a>
<div class="header">
<p>
Previous: <a href="Calling-a-Function-by-its-Name.html#Calling-a-Function-by-its-Name" accesskey="p" rel="prev">Calling a Function by its Name</a>, Up: <a href="Evaluation.html#Evaluation" accesskey="u" rel="up">Evaluation</a> &nbsp; [<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="Evaluation-in-a-Different-Context-1"></a>
<h3 class="section">9.2 Evaluation in a Different Context</h3>

<p>Before you evaluate an expression you need to substitute
the values of the variables used in the expression.  These
are stored in the symbol table.  Whenever the interpreter
starts a new function it saves the current symbol table
and creates a new one, initializing it with the list of
function parameters and a couple of predefined variables
such as <code>nargin</code>.  Expressions inside the function use the
new symbol table.
</p>
<p>Sometimes you want to write a function so that when you
call it, it modifies variables in your own context.  This
allows you to use a pass-by-name style of function,
which is similar to using a pointer in programming languages such
as C.
</p>
<p>Consider how you might write <code>save</code> and <code>load</code> as
m-files.  For example:
</p>
<div class="example">
<pre class="example">function create_data
  x = linspace (0, 10, 10);
  y = sin (x);
  save mydata x y
endfunction
</pre></div>

<p>With <code>evalin</code>, you could write <code>save</code> as follows:
</p>
<div class="example">
<pre class="example">function save (file, name1, name2)
  f = open_save_file (file);
  save_var (f, name1, evalin (&quot;caller&quot;, name1));
  save_var (f, name2, evalin (&quot;caller&quot;, name2));
endfunction
</pre></div>

<p>Here, &lsquo;<samp>caller</samp>&rsquo; is the <code>create_data</code> function and <code>name1</code>
is the string <code>&quot;x&quot;</code>, which evaluates simply as the value of <code>x</code>.
</p>
<p>You later want to load the values back from <code>mydata</code>
in a different context:
</p>
<div class="example">
<pre class="example">function process_data
  load mydata
  &hellip; do work &hellip;
endfunction
</pre></div>

<p>With <code>assignin</code>, you could write <code>load</code> as follows:
</p>
<div class="example">
<pre class="example">function load (file)
  f = open_load_file (file);
  [name, val] = load_var (f);
  assignin (&quot;caller&quot;, name, val);
  [name, val] = load_var (f);
  assignin (&quot;caller&quot;, name, val);
endfunction
</pre></div>

<p>Here, &lsquo;<samp>caller</samp>&rsquo; is the <code>process_data</code> function.
</p>
<p>You can set and use variables at the command prompt
using the context &lsquo;<samp>base</samp>&rsquo; rather than &lsquo;<samp>caller</samp>&rsquo;.
</p>
<p>These functions are rarely used in practice.  One
example is the <code>fail (&lsquo;<samp>code</samp>&rsquo;, &lsquo;<samp>pattern</samp>&rsquo;)</code> function
which evaluates &lsquo;<samp>code</samp>&rsquo; in the caller&rsquo;s context and
checks that the error message it produces matches
the given pattern.  Other examples such as <code>save</code> and <code>load</code>
are written in C++ where all Octave variables
are in the &lsquo;<samp>caller</samp>&rsquo; context and <code>evalin</code> is not needed.
</p>
<a name="XREFevalin"></a><dl>
<dt><a name="index-evalin"></a><em></em> <strong>evalin</strong> <em>(<var>context</var>, <var>try</var>)</em></dt>
<dt><a name="index-evalin-1"></a><em></em> <strong>evalin</strong> <em>(<var>context</var>, <var>try</var>, <var>catch</var>)</em></dt>
<dd><p>Like <code>eval</code>, except that the expressions are evaluated in the context
<var>context</var>, which may be either <code>&quot;caller&quot;</code> or <code>&quot;base&quot;</code>.
</p>
<p><strong>See also:</strong> <a href="Evaluation.html#XREFeval">eval</a>, <a href="#XREFassignin">assignin</a>.
</p></dd></dl>


<a name="XREFassignin"></a><dl>
<dt><a name="index-assignin"></a><em></em> <strong>assignin</strong> <em>(<var>context</var>, <var>varname</var>, <var>value</var>)</em></dt>
<dd><p>Assign <var>value</var> to <var>varname</var> in context <var>context</var>, which
may be either <code>&quot;base&quot;</code> or <code>&quot;caller&quot;</code>.
</p>
<p><strong>See also:</strong> <a href="#XREFevalin">evalin</a>.
</p></dd></dl>



<hr>
<div class="header">
<p>
Previous: <a href="Calling-a-Function-by-its-Name.html#Calling-a-Function-by-its-Name" accesskey="p" rel="prev">Calling a Function by its Name</a>, Up: <a href="Evaluation.html#Evaluation" accesskey="u" rel="up">Evaluation</a> &nbsp; [<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>