Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mathematica: Export sequence of MatrixPlots

  1. Jun 20, 2011 #1
    I've constructed a visualisation of the solution of an equation over time as a sequence of MatrixPlots and I'd like to export this as a movie for use in a presentation, but whatever file extension I try, I either get an error message

    (e.g. "The Export element \!\(\"GraphicsList\"\) contains a malformed data \
    structure and could not be exported to \!\(\"AVI\"\) format.")

    or I get an image of the word "Null".

    I've constructed an easier example to demonstrate:

    s = NDSolve[{x'[t] == -y[t] - x[t]^2, y'[t] == 2 x[t] - y[t]^3,
    x[0] == y[0] == 1}, {x, y}, {t, 30}]

    Movie := Module[{cc, cmax, list, framerate, nn, minscale, maxscale},
    nn = 100;
    minscale = -1;
    maxscale = 2;
    framerate = 15;
    cmax = 2 framerate;
    list = {};

    Monitor[
    For[cc = 1, cc <= cmax, cc = cc + 0.5,
    Pause[1/framerate];
    list = Table[First[Evaluate[y[cc] /. s]], {i, n}];];,

    MatrixPlot[{list}, Frame -> None, ColorFunctionScaling -> False,
    ColorFunction -> (Blend[{{minscale, Black}, {maxscale,
    Green}}, #] &), AspectRatio -> .1, ImageSize -> 1000]]]


    Then I've been trying variants on Export["example.avi",Movie]

    Thanks for your help
     
  2. jcsd
  3. Jun 20, 2011 #2
    First: Is your {i, n} a typo and you really meant {i, nn}

    Second: Can you, just as an experiment since you are having problem getting a simple graphics object, get rid of your Monitor and just produce a single simple list of your plots? Perhaps something as simple as

    For[cc = 1, cc ≤ cmax, cc = cc + 0.5,
    MatrixPlot[{Table[First[Evaluate[y[cc] /. s]], {i, nn}]}];
    ];

    or possibly even something as simple as

    Table[MatrixPlot[{Table[First[Evaluate[y[cc] /. s]], {i, nn}]}], {cc, 1, cmax, 0.5}]

    Note: You may or may not need a Print or Show in that, depending on version.
    But can you at least confirm that you are correctly producing all the frames?
    Once you have that then can you successfully Export the list of frames?

    Once that is working then you can go wild with your desktop publishing the details.
     
    Last edited: Jun 20, 2011
  4. Jun 27, 2011 #3
    Yep, sorry I did mean {i,nn}.

    Using Table[MatrixPlot[{Table[First[Evaluate[y[cc] /. s]], {i, nn}]}], {cc, 1, cmax, 0.5}]
    I get the correct set of images that I need.

    I have no problems exporting those as a stack of images (which I could then of course group together into a movie in another program), but I hope to extend this to 2 dimensional plots consisting of multiple lists and I think it will be a lot more tricky to use this method in that case.

    I cannot export as an avi file directly
     
  5. Jun 27, 2011 #4
    "I did some stuff and some stuff is wrong" is nearly impossible to successfully diagnose and fix on the first try.

    Simplifying your problem and getting rid of distracting details, showing the minimum amount of information necessary for someone else to be able to understand and exactly reproduce the problem will help everyone here who is asking for help and who are trying to help.

    If I do this

    s = NDSolve[{x'[t] == - y[t] - x[t]^2, y'[t] == 2 x[t] - y[t]^3, x[0] == y[0] == 1}, {x, y}, {t, 30}]; nn = 100; framerate = 15; cmax = 2 framerate; f = Table[MatrixPlot[{Table[First[Evaluate[y[cc] /. s]], {i, nn}]}], {cc, 1, cmax, 0.5}]

    I get 59 individual plots, each a horizontal line, displayed, which I assume are your results. Note: As I said in a previous post, you may need Show or Print or not, depending on your version of Mathematica.

    If I then do this

    Export["x.avi", f]

    I find an x.avi file in my Mathematica folder and outside Mathematica an avi player seems happy to show a movie of frames of that horizontal line.

    Can you reproduce all this exactly?
    If this much is working and it is not what you need can you show exactly what you did and describe exactly what is wrong and exactly what you need?
     
    Last edited: Jun 27, 2011
  6. Jun 30, 2011 #5
    Sorry for the lack of details in my previous post - I wrote it in a bit of a rush.

    Copying what you wrote exactly, I reproduce your results and can generate the movie of images as you described. So I have removed the "monitor" element from my actual code and can now generate the avi movie I required using a list of plots.

    I think I will stick to this method, and keep the "monitor" function out of my code. When I try what I wrote in my original post

    s = NDSolve[{x'[t] == -y[t] - x[t]^2, y'[t] == 2 x[t] - y[t]^3,
    x[0] == y[0] == 1}, {x, y}, {t, 30}]

    Movie := Module[{cc, cmax, list, framerate, nn, minscale, maxscale},
    nn = 100;
    minscale = -1;
    maxscale = 2;
    framerate = 15;
    cmax = 2 framerate;
    list = {};

    Monitor[
    For[cc = 1, cc <= cmax, cc = cc + 0.5,
    Pause[1/framerate];
    list = Table[First[Evaluate[y[cc] /. s]], {i, n}];];,

    MatrixPlot[{list}, Frame -> None, ColorFunctionScaling -> False,
    ColorFunction -> (Blend[{{minscale, Black}, {maxscale,
    Green}}, #] &), AspectRatio -> .1, ImageSize -> 1000]]]

    Export["x.avi", Movie]

    I generate an avi file which opens to give a single image displaying the word "Null". Generating a list of plots instead bypasses this error.

    Thanks for your help
     
  7. Jun 30, 2011 #6
    Module will return the last expression inside the Module and your last expression is Monitor. From the documentation
    http://reference.wolfram.com/mathematica/ref/Monitor.html
    they don't state what Monitor returns, but it appears from the examples that it returns the result of the first argument. Your first argument is For[...] and For always returns Null. So that probably explains everything you have seen.

    If you had put the generation of the MatrixPlot as the first argument to Monitor and your cc as a variable to watch while it was generating the plots then I'm guessing you could have watched the value of cc and still been able to get the list of frames to export after it was finished.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Mathematica: Export sequence of MatrixPlots
Loading...