Message Boards Message Boards

0
|
5583 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Persist a module variable value between two calls to a function?

Posted 6 years ago

Hi I have just started using Mathematica, I am trying to plot a bar graph with two data sets, resulting graph has two bar graphs with one appearing on top of the other.

We have a custom Mathematica package to do this job. which has a function, with a module variable say (x) this function gets called twice for the two data sets, and each time (x) is getting set to a different value by some calculation in this function. is it possible to retain the value of (x) which gets set first time until it gets set to a different value second time

say in first time call x =1, I would like to some how persist this value of x when I call this function second time, so I can use it then

I have been going through Mathematica Language documentation online, and came to know about PERSISTVALUE, which I understand is like a session variable, is this something I can use,

I did try to use PERSISTVALUE in our Mathematica code but I am unable to get it working,

when I first set PersistValue['myvar'] = 1, and then try to read it back in the same call it doesn't show the value, with this problem I cannot even expect it to be retained until the next call.

Firstly can you let me know if I can use PersistValue for retaining variable values between calls to a function.

If not then is there any other way I can do this.

Thanks for your help in advance Nalina

POSTED BY: Nalina Vemparala
4 Replies

Hi Nalina,

I am not sure whether I am understanding you question correctly. Maybe this is what you need:

Module[{oldArg = 0},
 f[x_] := With[{xx = oldArg}, oldArg = x; {xx, x}]
 ]

Whenever you call f[x] it "knows" its former argument. This variable (oldArg) itself cannot be seen from outside, but from the functions (f[]) point of view it is global. This is a nice trick I learnt from @Leonid Shifrin (if I remember correctly).

Regards -- Henrik

POSTED BY: Henrik Schachner

Hi Henrik Thanks, I will try this out. That is what I want, I want to remember the oldArg between two function calls, supposing in first function call, we set oldArg =2, in the second function call I want to remember oldArg=2.

Thanks a lot Nalina

POSTED BY: Nalina Vemparala

I'm not at all sure what your goal is but as you are new to Mathematica I'm going to make some assumptions and offer the following. Make a function

f[x_] := x

So when the variable x = 1:

f[1]

1

and when the variable x = 2:

f[2]

2

Notice the function f is still f, not 1 or 2, and the Global definition of f is...

?f

Global`f

f

If you want to persist a unique result of f you should set it to a unique variable name.

f1 = f[1]
f2 = f[2]

1 and 2

these persist, unless you later change them, or Quit the kernel.

Hi Mitch Thanks a lot for taking time to reply to my query.

I will try your suggestion,

Thanks Nalina

POSTED BY: Nalina Vemparala
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract