Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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:

Manipulate[customFunction[data,variable],{variable,{0,1}}]

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
1  
You could always make the importing cell an initialisation cell? –  Verbeia Apr 4 '13 at 8:27
1  
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. –  su1 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)? –  su1 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

3 Answers 3

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:

  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! –  su1 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[]) &;
Manipulate[
 If[data =!= None,
  Manipulate[customFunction[data, variable], {variable, {0, 1}}],
  Panel["Please load data!"]
  ],
 {{data, None}, None},
 Control[Row[{
    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

 
discard

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.