help-octave
[Top][All Lists]
Advanced

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

Re: Exporting a variable to the global workspace from a C++ function


From: Jaroslav Hajek
Subject: Re: Exporting a variable to the global workspace from a C++ function
Date: Wed, 12 Aug 2009 15:44:00 +0200

On Wed, Aug 12, 2009 at 2:59 PM, Justin
Bare<address@hidden> wrote:
> I tried the function you gave me in this way inside a C++ function:
>    string c = "x";
>    const string& cref = c;
>    octave_value x = 9;
>    const octave_value& xref = x;
>
>    cout << cref << endl;
>    cout << xref.int_value() << endl;
>
>    set_global_value(cref, xref);
>    cout << get_global_value("x").int_value() << endl;
>
> Which prints out this in Octave:
> x
> 9
> 9
>
> But what I want to do is create the variable x in this C++ function and
> send it to the global workspace so that when I type "x" in the octave
> command line it will give me "x = 9" instead of "error: `x' undefined near
> line 39 column 1"
>
> Is there a way to do this?
> Thanks.
>
> Justin
>

Global variables are not by default visible from any workspaces; they
need to be imported using the "global command".

One possibility to do this completely from C++, using the 3.2.x API is
(not actually tested):

symbol_table::global_varref ("name") = value;
symbol_table::set_scope (symbol_table::top_scope); // only needed if
called from m-file function
symbol_table::insert ("name").mark_global (); // will overwrite local
variable if exists!

another option is to call eval("global x;"), but that needs to invoke
the parser and is thus slower.

hth

-- 
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz



reply via email to

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