help-octave
[Top][All Lists]
Advanced

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

Re: `clear variable` does not work ?


From: John W. Eaton
Subject: Re: `clear variable` does not work ?
Date: Fri, 01 Feb 2008 15:23:10 -0500

On 31-Jan-2008, Matthias Brennwald wrote:

| I can't offer a solution to your problem, but I can add a bit to the
| confusion. Consider this (with Octave 3.0 on Ubuntu Linux 7.10):
| 
| octave:1> global x = [1 2 3]
| octave:2> clear x
| octave:3> x
| error: `x' undefined near line 33 column 1
| 
| So, it looks like x has been cleared and does not exist anymore. But, if
| I continue with the following, this seems not to be the case:
| 
| octave:4> global x = [9 9 9]
| octave:5> x
| x =
| 
|    1   2   7
| 
| There are two things that confuse me:
| 1. Why is x not equal to [9 9 9]?
| 2. Why does Octave remember the previous value of x, although it has
| been cleared previously?
| 
| I don't believe this is a bug, because it is so fundamental. I guess
| this has something to do with me (and others) not understanding well how
| things work. Can someone enlighten me?

The statement

  global x

creates a local variable called X, and, if it does not already
exist, a variable X in the global namespace.  The local variable X is
linked to the global variable X.  If you then write

  clear x

you are clearing the local variable only.  You need to use

  clear all

or

  clear global x

to remove X from the global namespace.

Once a global variable is initialized with a statement like

  global x = 13

it can't be initialized again unless it is cleared from the global
namespace first.

Does that make it any clearer?

jwe


reply via email to

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