help-octave
[Top][All Lists]
Advanced

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

Re: doubts about functions declared in parse.h ...


From: Alberto Francisco Martin Huertas
Subject: Re: doubts about functions declared in parse.h ...
Date: Sun, 13 Mar 2005 16:25:07 +0100
User-agent: Internet Messaging Program (IMP) 3.2.2

Mensaje citado por "John W. Eaton" <address@hidden>:

> On 11-Mar-2005, Alberto Francisco Martin Huertas <address@hidden>
> wrote:
> 
> | Hello. I'm a spanish university student and I'm performing my final career
> | project with Octave. I'm developing a DLF (Dinamycally Loaded Function)
> for
> | Octave, and  I'm interested in to know if there is any function declared
> in
> | parse.h header file that allows you to parse and execute some octave
> script
> | file without modifying some other enviroment variables used before. For
> | example, if the following script file (called example.m) had the following
> | sentence:
> | 
> |     A=3;
> |  
> | and my DLD function were something like this:
> | 
> |       DEFUN_DLD(example_function, arguments, ,
> |           "a example function") { 
> |                 the_function_im_looking_for("example.m");
> |                 return eval_string("A",...); 
> |                 
> | 
> |      } 
> | 
> | I would like the following behaviour in a Octave session:
> | 
> | octave:1> A=1
> | A = 1
> | octave:2> example_function()
> | ans = 3
> | octave:2> A
> | A = 1
> 
> The following will do what you want, but why do you want this
> behavior?  What are you really trying to do?
> 
> #include <octave/oct.h>
> #include <octave/parse.h>
> 
> DEFUN_DLD (exfun, args, , "exfun (file, sym)")
> {
>   octave_value_list retval;
> 
>   if (args.length () == 2)
>     {
>       std::string fname = args(0).string_value ();
>       std::string sym = args(1).string_value ();
> 
>       if (! error_state)
>       {
>         source_file (fname);
> 
>         if (! error_state)
>           {
>             symbol_record *sr = curr_sym_tab->lookup (sym);
> 
>             if (sr)
>               retval(0) = sr->def ();
>             else
>               error ("exfun: symbol \"%s\" not found", sym.c_str ());
>           }
>       }
>     }
>   else
>     print_usage ("exfun");
> 
>   return retval;
> }
> 
> Example of using this:
> 
>   $ cat script.m
>   a = 2;
> 
>   octave:1> exfun ("script.m", "a")
>   ans = 2
>   octave:2> a
>   a = 2
> 
> 
> Note that the seemingly equivalent function
> 
>   function retval = exfun (file, sym)
>     if (nargin == 2)
>       source (file);
>       retval = eval (sym);
>     else
>       usage ("exfun (file, sym)");
>     endif
>   endfunction
> 
> does not work, because functions like this introduce a local scope and
> C++ functions do not.  This is normally not a source of confusion,
> because it is not common to source a file inside a C++ function.
> 
> jwe
> 
Hello. I'm interested into source a m file in a C++ DLF function with the
following behaviour:

$ cat script.m
a=3;
$ octave
octave:1> a=1
a=1
octave:2> exfun ("script.m", "a")
ans = 3
octave:3> a
a = 1

and the exfun C++ function you have provided to me has this behaviour:

$ cat script.m
a=3;
$ octave
octave:1> a=1
a=1
octave:2> exfun ("script.m", "a")
ans = 3
octave:3> a
a = 3

so I would like to source a script having a local scope in my C++ function. Is
that posible ?

Thanks for your help, Alberto. 








-------------------------------------------------------------
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]