Sophie

Sophie

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

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

<html lang="en">
<head>
<title>Evaluation in a Different Context - 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="Evaluation.html#Evaluation" title="Evaluation">
<link rel="prev" href="Calling-a-Function-by-its-Name.html#Calling-a-Function-by-its-Name" title="Calling a Function by its Name">
<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="Evaluation-in-a-Different-Context"></a>
<p>
Previous:&nbsp;<a rel="previous" accesskey="p" href="Calling-a-Function-by-its-Name.html#Calling-a-Function-by-its-Name">Calling a Function by its Name</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Evaluation.html#Evaluation">Evaluation</a>
<hr>
</div>

<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>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>Consider how you might write <code>save</code> and <code>load</code> as
m-files.  For example,

<pre class="example">     function create_data
       x = linspace (0, 10, 10);
       y = sin (x);
       save mydata x y
     endfunction
</pre>
   <p>With <code>evalin</code>, you could write <code>save</code> as follows:

<pre class="example">     function save (file, name1, name2)
       f = open_save_file (file);
       save_var(f, name1, evalin ("caller", name1));
       save_var(f, name2, evalin ("caller", name2));
     endfunction
</pre>
   <p class="noindent">Here, &lsquo;<samp><span class="samp">caller</span></samp>&rsquo; is the <code>create_data</code> function and <code>name1</code>
is the string <code>"x"</code>, which evaluates simply as the value of <code>x</code>.

   <p>You later want to load the values back from <code>mydata</code>
in a different context:

<pre class="example">     function process_data
       load mydata
       ... do work ...
     endfunction
</pre>
   <p class="noindent">With <code>assignin</code>, you could write <code>load</code> as follows:

<pre class="example">     function load (file)
       f = open_load_file (file);
       [name, val] = load_var (f);
       assignin ("caller", name, val);
       [name, val] = load_var (f);
       assignin ("caller", name, val);
     endfunction
</pre>
   <p class="noindent">Here, &lsquo;<samp><span class="samp">caller</span></samp>&rsquo; is the <code>process_data</code> function.

   <p>You can set and use variables at the command prompt
using the context &lsquo;<samp><span class="samp">base</span></samp>&rsquo; rather than &lsquo;<samp><span class="samp">caller</span></samp>&rsquo;.

   <p>These functions are rarely used in practice.  One
example is the <code>fail (&lsquo;</code><samp><span class="samp">code</span></samp><code>&rsquo;, &lsquo;</code><samp><span class="samp">pattern</span></samp><code>&rsquo;)</code> function
which evaluates &lsquo;<samp><span class="samp">code</span></samp>&rsquo; in the caller'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><span class="samp">caller</span></samp>&rsquo; context and <code>evalin</code> is not needed.

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

<div class="defun">
&mdash; Built-in Function:  <b>evalin</b> (<var>context, try, catch</var>)<var><a name="index-evalin-552"></a></var><br>
<blockquote><p>Like <code>eval</code>, except that the expressions are evaluated in the
context <var>context</var>, which may be either <code>"caller"</code> or
<code>"base"</code>. 
</p></blockquote></div>

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

<div class="defun">
&mdash; Built-in Function:  <b>assignin</b> (<var>context, varname, value</var>)<var><a name="index-assignin-553"></a></var><br>
<blockquote><p>Assign <var>value</var> to <var>varname</var> in context <var>context</var>, which
may be either <code>"base"</code> or <code>"caller"</code>. 
</p></blockquote></div>

<!-- DO NOT EDIT!  Generated automatically by munge-texi. -->
<!-- Copyright (C) 1996, 1997, 1999, 2000, 2002, 2003, 2004, 2007, 2008, 2009 -->
<!-- John W. Eaton -->
<!-- This file is part of Octave. -->
<!-- Octave is free software; you can redistribute it and/or modify it -->
<!-- under the terms of the GNU General Public License as published by the -->
<!-- Free Software Foundation; either version 3 of the License, or (at -->
<!-- your option) any later version. -->
<!-- Octave is distributed in the hope that it will be useful, but WITHOUT -->
<!-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -->
<!-- FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License -->
<!-- for more details. -->
<!-- You should have received a copy of the GNU General Public License -->
<!-- along with Octave; see the file COPYING.  If not, see -->
<!-- <http://www.gnu.org/licenses/>. -->
   </body></html>