From help-octave-request at bevo dot che dot wisc dot edu Fri Feb 15 04:17:46 2002 Subject: Re: Loading files from within c++ (feval) From: Douglas Eck To: Paul Kienzle CC: help-octave at bevo dot che dot wisc dot edu Date: Fri, 15 Feb 2002 11:16:37 +0100 This is a multi-part message in MIME format. --------------000903070203090303020500 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit 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 --------------000903070203090303020500 Content-Type: text/plain; name="Makefile" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="Makefile" ##Primitive but effective ##This Makefile is a very-crippled version of something ##I cribbed from Paul Kienzle's matcompat work (before it ##turned into octave-forge. ## ##Douglas Eck doug at idsia dot ch ##If you don't want debugging and proflining ##OPTIMIZATION = -O2 ##If you want debugging and profiling ##You will have to set the version by hand. OCTAVE_VERSION=2.1.35 OPTIMIZATION = -pg -g MKOCTFILE = mkoctfile -v CC=g++ CCFLAGS=-fno-implicit-templates -Wall CCINCLUDES=-I/usr/include/octave-$(OCTAVE_VERSION) -I/usr/include/octave-$(OCTAVE_VERSION)/octave LD_LIBS=-lblas -loctave -loctinterp -ldl -lcruft -lg2c -ltermcap -lhdf5 -lreadline -lkpathsea -lfftw LD_PATHS=-L/usr/lib/octave-$(OCTAVE_VERSION) -L/usr/src/octave-$(OCTAVE_VERSION)/kpathsea PROJ=tstFile OBJS=tstFile.o OCTS=tstFile.oct all: $(OBJS) $(OCTS) $(PROJ) $(PROJ): $(PROJ).o $(CC) $(OPTIMIZATION) $(OBJS) -o $(PROJ) $(LD_PATHS) $(LD_LIBS) .cc.o: $(CC) $(CCFLAGS) $(CCINCLUDES) $(OPTIMIZATION) -c $< %.oct: %.cc; $(MKOCTFILE) $< clean: rm $(OBJS) $(OCTS) $(APPS) --------------000903070203090303020500 Content-Type: text/x-c++src; name="tstFile.cc" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="tstFile.cc" /* Filename: tstFile.cc Overview: test Required args: none Optional args: none Return values: none Details: none Author: Douglas Eck (doug at idsia dot ch) http://www.idsia.ch/~doug Project: LSTM Long Short-Term Learning Time-stamp: <2002-02-15 11:00:41 doug> Copyright (C) 2001 Douglas Eck This program is free software and may be used for any purpose. Douglas Eck is not is not responsible for the consequences of using this software. */ #include #include #include #include #include void dotest() { octave_value_list func; func(0)="load"; curr_sym_tab = new symbol_table(10); install_builtin_function(func,"load","foo",0); Matrix a; char * fn = "/home/doug/src/tstFile/tst.mat"; char * var = "a"; octave_value_list args; args(0)=fn; args(1)=var; cout << "Loading file " << fn << " ... "; feval("load", args, 0); // no arguments returned cout << "success" << endl; cout << "Fetching variable (" << var << ") from symbol table... "; symbol_record *sr = curr_sym_tab->lookup(var); if (sr == NULL || sr->def().is_undefined ()) { cerr << "Variable (" << var << ") not found in symbol table " << fn << endl; exit(0); } else { a = sr->def().matrix_value(); } cout << "success" << endl; cout << "Displaying matrix "<< endl << a << endl; } DEFUN_DLD (tstFile, args, , "testing") { octave_value_list retval; dotest(); return retval; } int main(int argc, char * argv[]) { global_sym_tab=new symbol_table(2048); dotest(); } --------------000903070203090303020500-- ------------------------------------------------------------- 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 -------------------------------------------------------------