Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using an initialization cell to import a lot of data:

data = Import[myFiles];

and then I use a custom function to display this data with Manipulate:


I have to keep these two commands in two seperate cells, because importing the data in customFunction each time I manipulate variable would take a lot of time.

The problem is, when I start my notebook, Manipulate is evaluated automatically and sends a lot of error messages, because data is not defined. I know it might go against what Manipulate is made for, but is there a way to prevent Manipulate from being evaluated until I import my data for the first time?

share|improve this question
You could always make the importing cell an initialisation cell? – Verbeia Apr 4 '13 at 8:27
Can't you use SaveDefinitions->True ? – b.gatessucks Apr 4 '13 at 8:50
@Verbeia no because initialization cells are evaluated only just before a first cell is evaluated in the notebook, whereas Manipulate is evaluated right away. – Sulli Apr 4 '13 at 9:24
@b.gatessucks I didn't know this option and it looks like it does what I want! So basically with this option I would only need to evaluate data = Import[myFiles]; once and then again only if myFiles change? Is there a bad side-effect of saving a lot of information with SaveDefinitions -> True (in terms of time for the notebook to load for example)? – Sulli Apr 4 '13 at 9:27
@su1 My understanding is that with that option Manipulate will remember the last state it was in, including all controls. As to performance issues, you just have to try and see. – b.gatessucks Apr 4 '13 at 9:35

I see three methods:

1. ValueQ

Keep the two separate cells and make the last dependent on data. If data has no value for any reason, the Manipulate-part won't be evaluated at all. For sake of consistency, I've included a first cell that clears any previous value of data, but this (just as the second cell) should be only evaluated once, at the start of your session.

 In[1] := ClearAll[data];

 In[2] := data = Import[myFiles];

 In[3] := If[ValueQ[data], Manipulate[...], "do nothing"]

2. Initialization option

Create just one cell, but put in the option Initialization which ensures that its content gets evaluated before displaying the body of Manipulate:

     customFunction[data, variable], {variable, {0, 1}},
     Initialization :> (data = Import[myFiles])]

Tough note that his will load myFiles at each time the Manipulate is redisplayed.

3. Initialization cell

Keep the two cells and make the first one an Initialization cell (like Verbeia said) (right click on cell bracket >> Initialization Cell. From now on, any time you open this notebook, the initialization cell will be evaluated automatically.

share|improve this answer
Probably the first option would be the best for me. "Initialization option" is not good enough because myFiles take a few seconds to be imported, so it's better to import them only once. "Initialization cells" are not good enough because they are not evaluated automatically at start, they are evaluated only after I evaluate any cell and click "Yes" when asked if I want to evaluate all initialization cells, whereas the cell containing Manipulate is evaluated automatically! – Sulli Apr 4 '13 at 9:16

Here is an alternative which encapsulates everything in a graphical user interface and needs no global variables:

longimport = (Pause[5]; RandomReal[]) &;
 If[data =!= None,
  Manipulate[customFunction[data, variable], {variable, {0, 1}}],
  Panel["Please load data!"]
 {{data, None}, None},
    Button[Dynamic[If[data === None, "Load Data", "Reload Data"]], 
     data = longimport[], Method -> "Queued"],
    Button["Clear Data", data = None, Method -> "Queued"]
 AppearanceElements -> None

You can of course use whatever other expression you prefer instead of None to indicate that the data has not yet been imported. The custom longimport function is of course just for testing purposes, you could also let the user select a filename and then import within such a function...

share|improve this answer

This did it for me:

Manipulate[customFunction[data,variable], {variable,{0,1}}, SaveDefinitions->True]

No performance side affects here.

Thanks to user b.gatessucks for providing this answer in the question's comments.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.