Mathematica: Export sequence of MatrixPlots

In summary: AspectRatio -> .1, ImageSize -> 1000]]]I get 59 individual plots, each a horizontal line, displayed, which I assume are your results.If I then do thisExport["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.
  • #1
musicgirl
12
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
  • #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:
  • #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
 
  • #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:
  • #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
 
  • #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.
 

Related to Mathematica: Export sequence of MatrixPlots

1. How do I export a sequence of MatrixPlots in Mathematica?

To export a sequence of MatrixPlots in Mathematica, you can use the Export function followed by the sequence of MatrixPlots and the desired file format. For example, Export["sequence.gif", {MatrixPlot1, MatrixPlot2, MatrixPlot3}, "GIF"] will export a sequence of three MatrixPlots as a GIF file.

2. Can I customize the appearance of the exported MatrixPlots?

Yes, you can customize the appearance of the exported MatrixPlots by using the options available in the MatrixPlot function. These options include changing the color scheme, adding labels and legends, adjusting the size and resolution, and more.

3. How can I export the MatrixPlots as individual files?

To export the MatrixPlots as individual files, you can use the Table function to generate a list of MatrixPlots with different parameters, and then use the Export function to export each plot as a separate file. For example, Table[Export["matrixplot"<>ToString[i]<>".png", MatrixPlot[i]], {i, 1, 10}] will export 10 MatrixPlots as individual PNG files.

4. Is it possible to export a sequence of MatrixPlots in a specific order?

Yes, it is possible to export a sequence of MatrixPlots in a specific order by using the Sort function. You can sort the list of MatrixPlots based on a specific parameter, such as the values in the matrix or the labels, before exporting them.

5. Can I export the MatrixPlots in different file formats?

Yes, you can export the MatrixPlots in different file formats such as PNG, JPEG, GIF, PDF, and more. You can specify the desired file format in the Export function, and Mathematica will convert the MatrixPlots accordingly.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
420
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
415
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
52
Views
12K
Replies
1
Views
930
Back
Top