[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Retained Value
From: |
Mike Miller |
Subject: |
Re: Retained Value |
Date: |
Wed, 13 Jul 2016 09:16:27 -0700 |
User-agent: |
Mutt/1.6.0 (2016-04-01) |
On Wed, Jul 13, 2016 at 08:44:58 -0700, Thomas D. Dean wrote:
> I have some code, that unless I clear all, does not change value when
> changed.
>
> X=[0,38000]
> Y=[0,3000]
> poly_deg = 4
> global P = polyfit(X,Y,poly_deg)
> P
>
> X=[0,38000]
> Y=[0,3000]
> poly_deg = 8
> global P = polyfit(X,Y,poly_deg)
> P
>
> P has the same value as before.
>
> What am I doing wrong?
The "global" keyword is not meant to be used with assignment. You should
declare a name as "global" and then assign to it. The global attribute
is retained any time the name is assigned to in the same scope.
As a shortcut Octave allows you to do "global foo" and "foo = 0" in one
expression, but the assignment only has an effect when the variable is
declared the first time.
Safest to stick to the Matlab compatible syntax of
global P
P = polyfit (...
P = something else
...
--
mike