view examples/code/globaldemo.cc @ 27595:18fea1c5b21a stable

doc: improve example of using global variables from oct-files. * examples/code/globaldemo.cc: Adapt file to the changes from cset b29904962d2d. * doc/interpreter/external.txi: Overhaul text to match the current implementation. With cset b29904962d2d a new way of accessing global variables via the interpreter's symbol table was introduced. Because of this, a recent question on the help mailing-list could not be sufficiently answered by pointing at the Octave documentation. https://lists.gnu.org/archive/html/help-octave/2019-10/msg00260.html
author Kai T. Ohlhus <k.ohlhus@gmail.com>
date Thu, 31 Oct 2019 14:48:22 +0900
parents 73ab962bc52d
children
line wrap: on
line source

#include <octave/oct.h>
#include <octave/interpreter.h>

DEFMETHOD_DLD (globaldemo, interp, args, , "Global Demo")
{
  if (args.length () != 1)
    print_usage ();

  octave_value retval;

  std::string s = args(0).string_value ();

  octave::symbol_table& symtab = interp.get_symbol_table ();

  octave_value tmp = symtab.global_varval (s);

  if (tmp.is_defined ())
    retval = tmp;
  else
    retval = "Global variable not found";

  symtab.global_assign ("a", 42.0);

  return retval;
}