# Refresh dynamic variable when used with Get

I came across this issue with a more complicated program I was working on.

Suppose we have a volatile variable data:

In[1]:= data = RandomReal[{0,1},{10}];


As this cell is evaluated,

In[2]:= Dynamic@data


refreshes as expected.

However, if we want to save a certain instance of data and later load it in:

In[3]:= DumpSave["filepath.mx",data]


(change value of data to something else)

In[4]:= <<"filepath.mx"


the dynamic cell (In[2]) isn't updated (unless you evaluate it again). It can be forced to update on the loading of the .mx file by using a Refresh[...,UpdateInterval->time], but it seems odd that re-evaluating In[1] instantly changes the dynamic cell while redefining data with the .mx file doesn't.

Any explanation for this? And is using the Refresh method a good solution here?

-
I always figured that Update was intended to be used exactly in situations like this, however it doesn't seem to work. – jVincent May 6 '13 at 14:58

I have no insight into the internals but have found several situations where for whatever reasons the automatic determination of the necissity to update fails. I would wish there would be a programmatic possibility to tell Mathematica that a ceratin symbol has changed, but don't know about that. Of course you could do something stupid like this to inforce an update:

 Module[{datax = data}, data =.; data = datax]


Another workaround would be to use Export and Import instead of DumpSave and Get:

 Export["filepath.mx", data];


and then do:

 data = Import["filepath.mx"]


Here the explicit Set for data will trigger the update of the Dynamic.

-

Often I do something kludgy like

Dynamic[foo; data]

<<"filepath.mx"; foo++


It's somewhat similar to Albert Retey's first solution. However, foo is a global variable and data can be an arbitrary piece of code.

Here's a way to dress it up. It does the same thing, but it reads better.

ClearAll[dependsOn, update];
SetAttributes[dependsOn, HoldAll];
SetAttributes[update, HoldAll];
dependsOn[sym_Symbol] := (
dependsOn[sym] = 0;
update[sym] := (dependsOn[sym] = 1 - dependsOn[sym];))

data = RandomReal[{0, 1}, {10}];
DumpSave["filepath.mx",data]

Dynamic[dependsOn[data]; data]

data = RandomReal[{0, 1}, {10}];

<< "filepath.mx"; update[data]

-