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 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! *)
vizData[data]

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.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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}]
share|improve this answer
    
Works great! When it is advisable to use Hold[] and when not? –  Robinaut Jul 24 at 11:46
    
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. –  m_goldberg Jul 24 at 17:20

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.