[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to use existing variable
From: |
Leo Butler |
Subject: |
Re: How to use existing variable |
Date: |
Thu, 4 Nov 2010 21:10:52 +0000 (GMT) |
User-agent: |
Alpine 2.00 (DEB 1167 2008-08-23) |
On Fri, 5 Nov 2010, Neil Ghosh wrote:
< Hi,
<
< I have a variable X=csvread('XXXX'); with huge data in it.
<
< My function is written in a .m fileĀ "MyPrg.m" and it uses the data in X and
have reference to that variable.
<
< Whenever I call MyPrg in command line it give me error 'X' is undefined ,
however I dont have "clear" in my function
< It should recognize the global variable X.
<
< I can not afford to read that huge file everytime in my function code.
<
< Please help
You should look at how to use a global or persistent variable.
Here is a function that simply memorises the arguments it
has seen, except when G is empty, when something special
is done. You could replace this with your cvsread command.
function y = fng(x)
global G;
if isempty(G)
G=1; # this only gets executed once
endif
y=G=[x,G]; # now do something...
endfunction
Note that until G is declared global outside of the function
scope, G behaves like a persistent variable local to the
function and you will get an error message if you try to
use G outside of the function.
Leo
====
octave> function y = fng(x)
> global G;
> if isempty(G)
> G=1;
> endif
> y=G=[x,G];
> endfunction
octave> fng(2)
ans =
2 1
octave> G
error: `G' undefined near line 65 column 1
octave> fng(3)
ans =
3 2 1
octave> global G;
octave> G
G =
3 2 1
--
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.