[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: creating built-in variables and extracting formulas from anonymous f
From: |
Geordie McBain |
Subject: |
Re: creating built-in variables and extracting formulas from anonymous functions |
Date: |
Fri, 16 Mar 2007 09:49:53 +1100 |
On Thu, 2007-03-15 at 13:14 -0700, Eric S. Carlson wrote:
> 1. Is there some way to define and initialize a built-in variable
> dynamically at startup? Or is there another type of variable I can use that
> does not get erased with the clear command?
One way is to fake a variable with a function in an m-file. The file
will still be there to read after a clear. You can even change the
value of the variable by hiding a persistent variable inside the
function. Admittedly this does have a different syntax to the usual
variable assignment.
Example. Say before you start Octave you add
%<---
eulermascheroni1 = 0.577215664901532860606512;
%<---
to the file .octaverc in your working directory and create the file
eulermascheroni2.m:
%<---
function g = eulermascheroni2 (x)
persistent g0 = 0.577215664901532860606512
if nargin>0, g0=x; endif
g = g0;
endfunction
%<---
then in Octave:
%<---
octave:1> eulermascheroni1
eulermascheroni1 = 0.57722
octave:2> eulermascheroni2
ans = 0.57722
octave:3> clear
octave:4> eulermascheroni1
error: `eulermascheroni1' undefined near line 4 column 1
octave:4> eulermascheroni2
ans = 0.57722
octave:5> eulermascheroni2 (0.58)
ans = 0.58000
octave:6> clear
octave:7> eulermascheroni2 (0.58)
ans = 0.58000
octave:8>