help-octave
[Top][All Lists]
Advanced

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

Re: How to share variables between functions


From: John W. Eaton
Subject: Re: How to share variables between functions
Date: Tue, 22 May 2007 17:20:42 -0400

On 22-May-2007, Chris Wagner wrote:

| John W. Eaton wrote:
| > 
| > 
| > With Octave 2.9.10, you can write
| > 
| >   function y = f (x)
| >     y = quad (@(t) g (x, t), 0, 1);
| >   endfunction
| >   function y = g (x, t)
| >     y = t*x;
| >   endfunction
| > 
| > jwe
| > 
| > 
| 
| I may be missing something, but I can't seem to get the above anonymous
| function passing to work between multiple .m files.
| 
| This seems to work (everything in one .m file):
| f2.m:
|   function y = f2(x)
|     y = f3(@(t) g(x,t), 1, 2);
| 
|   function y = g(x,t)
|     y = t*x;
| 
|   function y = f3(infunc, a, b)
|     y = infunc(a) + infunc(b);
| 
| Then on the command line, 
| octave:5> f2(5)
| ans =  15
| 
| But with the function f3 in another .m file in the same directory:
| f2.m:
|   function y = f2(x)
|     y = f3(@(t) g(x,t), 1, 2);
| 
|   function y = g(x,t)
|     y = t*x;
| 
| f3.m:
|   function y = f3(infunc, a, b)
|     y = infunc(a) + infunc(b);
| 
| And running on the command line:
| octave:7> f2(5)
| error: `g' undefined near line 2 column 15
| error: called from `?unknown?'
| error: evaluating binary operator `+' near line 2, column 20
| error: evaluating assignment expression near line 2, column 8
| error: called from `f3' in file `/home/cwagner/octave-test/f3.m'
| error: evaluating assignment expression near line 2, column 5
| error: called from `f2' in file `/home/cwagner/octave-test/f2.m'
| octave:7>
| 
| Is this the expected behavior? I'm using Octave 2.9.11.

I think it should work.

Please try the following patch.

BTW, it is best to report bugs to the bugs list.

Thanks,

jwe


src/ChangeLog:

2007-05-22  John W. Eaton  <address@hidden>

        * pt-fcn-handle.cc (tree_anon_fcn_handle::rvalue):
        Set current function as parent of anonymous function handle.


Index: src/pt-fcn-handle.cc
===================================================================
RCS file: /cvs/octave/src/pt-fcn-handle.cc,v
retrieving revision 1.7
diff -u -u -r1.7 pt-fcn-handle.cc
--- src/pt-fcn-handle.cc        6 Apr 2007 16:11:38 -0000       1.7
+++ src/pt-fcn-handle.cc        22 May 2007 21:19:57 -0000
@@ -115,6 +115,11 @@
     = new octave_user_function (new_param_list, new_ret_list,
                                new_cmd_list, new_sym_tab);
 
+  octave_function *curr_fcn = octave_call_stack::current ();
+
+  if (curr_fcn)
+    uf->stash_parent_fcn_name (curr_fcn->name ());
+
   uf->mark_as_inline_function ();
 
   octave_value ov_fcn (uf);

reply via email to

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