[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

global variables in .oct-files

From: John W. Eaton
Subject: global variables in .oct-files
Date: Thu, 2 Oct 1997 01:02:10 -0500

On 27-Sep-1997, Stef Pillaert BK <address@hidden> wrote:

| I'm trying to translate some of my octave-functions (.m-files) into C++
| (.oct-files) to speed up things. I use a lot of global variables though,
| otherwise I have to pass a lot of parameters to each function. Is there a
| way to "see" those global variables in my C++-code?

Yes, it's possible.  Here is an example of how you can get and set
global values in .oct files:

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

  get_global_value (const string& nm)
    octave_value retval;

    symbol_record *sr = global_sym_tab->lookup (nm);

    if (sr)
        octave_value val = sr->variable_value ();

        if (val.is_undefined ())
          error ("get_global_value: undefined symbol `%s'", nm.c_str ());
          retval = val;
      error ("get_global_value: unknown symbol `%s'", nm.c_str ());

    return retval;

  set_global_value (const string& nm, const octave_value& val)
    symbol_record *sr = global_sym_tab->lookup (nm, true);

    if (sr)
      sr->define (val);
      panic_impossible ();

  DEFUN_DLD (foo, args, ,
    "foo (global_variable_name [, val])")
    octave_value_list retval;

    int nargin = args.length ();

    if (nargin == 1 || nargin == 2)
        string var = args(0).string_value ();

        if (! error_state)
            if (nargin == 2)
              set_global_value (var, args(1));
              retval(0) = get_global_value (var);

    return retval;

If the function defined above is called with two values, it sets the
value of the named global variable to the value of the second
argument.  If called with just one argument, it finds the value of the
named global variable:

  $ octave
  Octave, version 2.0.9 (alpha-dec-osf3.2).
  Copyright (C) 1996, 1997 John W. Eaton.
  This is free software with ABSOLUTELY NO WARRANTY.
  For details, type `warranty'.

  octave:1> x = 2
  x = 2
  octave:2> foo ("x")
  error: get_global_value: unknown symbol `x'
  error: evaluating index expression near line 2, column 1
  octave:2> global x = 2
  octave:3> foo ("x")
  ans = 2
  octave:4> foo ("y", 3)
  octave:5> function f () global y; y, end
  octave:6> f
  y = 3

Functions like this will be provided in some future version of
Octave (perhaps 2.0.10).



reply via email to

[Prev in Thread] Current Thread [Next in Thread]