From help-octave-request at bevo dot che dot wisc dot edu Mon Oct 6 05:22:40 1997 Subject: Re: global variables in .oct-files From: Stef Pillaert To: help-octave at bevo dot che dot wisc dot edu Date: Mon, 06 Oct 1997 12:22:54 +0200 At 01:02 2/10/97 -0500, you wrote: >On 27-Sep-1997, Stef Pillaert BK 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 > #include > > octave_value > 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 ()); > else > retval = val; > } > else > error ("get_global_value: unknown symbol `%s'", nm.c_str ()); > > return retval; > } > > void > set_global_value (const string& nm, const octave_value& val) > { > symbol_record *sr = global_sym_tab->lookup (nm, true); > > if (sr) > sr->define (val); > else > 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)); > else > 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: > This works great, but what I want to do now is: just changing a few elements in a large (global) variable. So I'm really looking for a translation of what in a .m-file would be: function f1() global matrixa matrixa(1,1)=10000; end (yes, I actually use a function as if it was a "procedure"). I tried to translate this into C++ with something like: octave_value val=get_global_value("matrixa"); Matrix matrixb = val.matrix_value(); matrixb(1,1) = 10000; octave_value val2=octave_value(matrixb); set_global_value("matrixa",val2); This doesn't work (octave crashes on it), and even if it would work, I presume there is an unnecessary copy of the global variable. I tried to write a C++ function "change_global_value" (or should it be "change_some_elements_in_global_value" ?), but I'm affraid my knowledge of C++ is to limited and I get into trouble with pointers and stuff... Anyone with a few minutes of time to help me out? Thanks, Stef.