help-octave
[Top][All Lists]
Advanced

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

Re: Loading files from within c++ (feval)


From: Paul Kienzle
Subject: Re: Loading files from within c++ (feval)
Date: Fri, 15 Feb 2002 10:01:58 -0500

Try the following (slightly tidied) version:

#include <octave/oct.h>
#include <octave/parse.h>
#include <octave/variables.h>


/* modeled on get_global_value in variables.cc; perhaps this
   should be there too */
octave_value
get_value (const std::string& nm)
{
  octave_value retval;

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

  if (sr)
    {
      octave_value sr_def = sr->def ();

      if (sr_def.is_undefined ())
        error ("get_value: undefined symbol `%s'", nm.c_str ());
      else
        retval = sr_def;
    }
  else
    error ("get_value: unknown symbol `%s'", nm.c_str ());

  return retval;
}


void dotest() {

  octave_value_list args;
  args(0)="tst.mat";
  args(1)="a";

  cout << "Loading file " << args(0).string_value() << " ... ";
  feval("load", args, 0); // no arguments returned
  cout << "success" << endl;

  cout << "Fetching variable (" << args(1).string_value() << ") from symbol 
table... ";
  octave_value a = get_value("a");

  if (!error_state) {
    cout << "success" << endl;
    cout << "Displaying value "<< endl;
    a.print(cout);
  }
}


extern DECLARE_FUN(load,args,nargout);
int main(int argc, char * argv[]) {
  initialize_symbol_tables();
  install_builtin_function(Fload,"load","foo",0);

  dotest();
}


Paul Kienzle
address@hidden

On Fri, Feb 15, 2002 at 11:16:37AM +0100, Douglas Eck wrote:
> Paul Kienzle wrote:
> 
> > Not having looked at the relevant code, I'm guessing that load is
> > attempting to create a variable in the current symbol table but
> > failing because you haven't initialized the current symbol table.
> > Maybe something like:
> >     octave_value a;
> >     curr_sym_tab = new symbol_table(10);
> >     ... your feval stuff ...
> >     symbol_record *sr = curr_sym_tab->lookup("a");
> >     if (sr && sr->is_defined ()) a = sr->def ();
> >          
> 
> 
> 
> Thanks Paul. That helped. Now all I need to do is populate
> the symbol table with the functions I need... namely
> "load" and, when that works, "save".  I tried to figure out how
> install_builtin_function(...) works. I think that's what I need to use.
> But for the life of me I can't figure out how to call the constructor.
> John uses clever macro stuff
>   [ builtins.cc: XDEFUN_INTERNAL (  load  ,     args  ,     nargout  ,   true 
> ...]
> that Doug doesn't understand.
> 
> Any help?
> 
> Attached is complete code with Makefile.
> 
> Thanks!
> Doug
> 



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

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