Mathematica Mathematica: Export sequence of MatrixPlots

AI Thread Summary
The discussion revolves around exporting a sequence of MatrixPlots as a movie in Mathematica, with users encountering errors when attempting to export to AVI format. The initial poster provides a code snippet using NDSolve to generate plots but receives a "Null" image when exporting. Suggestions include simplifying the code by removing the Monitor function and ensuring the correct structure of the data being exported. After adjustments, the poster successfully generates individual plots and exports them as a movie, confirming that the Monitor function was causing the issue by returning Null. The conversation emphasizes the importance of clarity in code and the need for reproducible examples when seeking help.
musicgirl
Messages
12
Reaction score
0
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
 
Physics news on Phys.org
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:
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
 
"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:
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
 
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.
 

Similar threads

Replies
1
Views
2K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
4
Views
3K
Replies
3
Views
5K
Replies
2
Views
4K
Replies
52
Views
12K
Replies
2
Views
2K
Back
Top