I'm working with large data sets (>1gb) which I'm visualizing with Manipulate[]. The following example works fine for me with no additional memory overhead:

data = RandomReal[{0, 1}, {500, 500, 500}];
(* no memory overhead and fast response time *)
Manipulate[Image[data[[All, All, i]]], {i, 1, 500, 1}]

However, if I wrap the above code into a separate function definition, memory usage rises and evaluation does'n complete in a reasonable time:

vizData[dataVar_] := Manipulate[Image[dataVar[[All, All, i]]],{i, 1, 500, 1}]
(* very high memory usag and slow evaluation! *)

How can I wrap the functionality of Manipulate into a function without this high memory utilization? Working with global variables is not an option because I'm dealing with multiple data sets.


You don't want to pass the full data set into the Manipulate. You just want to pass its name and have it evaluated inside the Manipulate. Try the following. Is it fast enough?

SeedRandom[42]; data = RandomReal[{0, 1}, {500, 500, 500}];
SetAttributes[vizData, HoldFirst];
vizData[dataVar_Symbol] := 
  Manipulate[Image[dataVar[[All, All, i]]], {i, 1, 500, 1}]
  • $\begingroup$ Works great! When it is advisable to use Hold[] and when not? $\endgroup$ – Robinaut Jul 24 '14 at 11:46
  • $\begingroup$ I didn't use Hold. I gave your function the attribute HoldFirst, which is something different. I did it because Manipulate has the attribute HoldAll, which made me suspicious about passing in the data by value. I was acting on intuition, not out of a deep understanding of Manipulate internals. $\endgroup$ – m_goldberg Jul 24 '14 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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