help-octave
[Top][All Lists]
Advanced

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

Generating global variables locally


From: John W. Eaton
Subject: Generating global variables locally
Date: Mon, 4 Oct 1999 12:56:30 -0500 (CDT)

On  3-Oct-1999, Vivek Shanmuganathan \(95410006-BS\) <address@hidden> wrote:

| Is it possible
| to generate, from within a function, a large number of global variables
| and assigning values to them? And, it should also be possible to destroy
| those global names as and when required by calling a similar function.
| 
| For example:
| 
| ---------------------------
| function generate_global_vars(a_vector)
| # a_vector is a vector whose size is unknown.
| 
| for val = ext
| 
| # Here I want to generate global variables 
| # and assign values to them. The names and number of these global variables
| # will be different based on the size and the value of the vector. For
| # example, for a_vector = [10:5:30], I would like to create new global
| # variables y10, y15, y20, etc.
| 
| endfor
| 
| endfunction

Something like

  function generate_global_vars (a_vector)
    for i = a_vector
      eval (sprintf ("global y%d = 0;", i));
    endfor
  endfunction

should do it.  To see these variables in the top-level workspace,
you'll have to evaluate the global commands there too, though you only
need to give them values in one place.

You should be able to construct similar clear commands, but to really
make the variables go away, you'll have to clear any functions that
reference the global variables (i.e., generate_global_vars and any
other functions where you've declared them global) and you'll have to
clear them in the top-level workspace (if you've made them visible
there).

jwe



---------------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.  To ensure
that development continues, see www.che.wisc.edu/octave/giftform.html
Instructions for unsubscribing: www.che.wisc.edu/octave/archive.html
---------------------------------------------------------------------



reply via email to

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