# Loop for saving images with plots of numerical data to make a movie

I am having a problem with construction of a loop that would load data from a bunch of files one by one, then produce a plot for each of the files, then save each of the plots as a separate image file, and then make a movie from the resulting bunch of images.

INTRODUCTION. Suppose there is one file with numerical data in a local directory. The file contains numerical data, for example a two-dimensional function "n1". One can import the data from the file using this command:

n1 = Import["1ExampleGraphix.h5", {"Datasets", "/n1"}];


Then one can plot the data using ListDensityPlot and save the resulting plot as a jpg file "1image.jpg":

Export["1image.jpg", ListDensityPlot[n1, ColorFunction -> "Rainbow"]]


PART I: LOADING, PLOTTING AND SAVING FILES ONE BY ONE. Now suppose there is a bunch of 100 files in a local directory:

1ExampleGraphix.h5

...

100ExampleGraphix.h5

and one ultimately wants to make a movie from this sequence.

The strategy is to load the files one by one, make a plot from each file, and save the corresponding image to a new file. One needs to generate a new filename according to the loop number. This loop is supposed to generate a sequence of files with images:

1image.jpg

...

100image.jpg

My problem is that I cannot load a file with the filename generated by ToString. Does anyone know how to do this correctly? I am trying the following loop:

For[i = 1, i < 2,
i++, {
(*The next line creates a string with the filename to be

(*The next line loads the data for making a plot*)

(*The next line creates a string with the filename to be saved*)

savefilename = ToString[i] "image.jpg";

(*The next line saves the plotted data as a jpg image*)

Export[savefilename,
ListDensityPlot[argPsi1, ColorFunction -> "Rainbow"]]}]


The error message is:

Import::chtype: "\!$$\* StyleBox[\"\\\"First argument \\\"\", \"MT\"]$$\!$$\* StyleBox[ RowBox[{\"\\\"1\\\"\", \" \", \"\\\"ExampleGraphix.h5\\\"\"}], \"MT\"]$$\!$$\* StyleBox[\"\"\", \"MT\"]$$ is not a valid file, directory, or URL specification.


PART II: CREATING A MOVIE FROM THE SEQUENCE OF IMAGES. The second question is: How to make sure that the files are arranged in frames in the correct order, when the movie is created?

I am trying the following commands:

files = FileNames["*.jpg"];
images = Map[Import, files];
Export["video.avi", images]


The first line picks all .jpg files from the directory, but it is unclear how are the frames sorted in the resulting video file?

Any help would be greatly appreciated.

With best wishes, Dmitry

• Try Do[With[{s = IntegerString[k]}, Export[s <> "image.jpg", ListDensityPlot[Import[s <> "ExampleGraphix.h5", {"Datasets", "/n1"}], ColorFunction -> "Rainbow"]]], {k, 1, 100}] Sep 28, 2018 at 17:26
• Thank you very much for the answer. Your code has solved my problem! Sep 28, 2018 at 19:35

You may use StringTemplate for the file names. You can also take advantage of parallel processing with ParallelMap instead of the procedural For.

LaunchKernels[];
ParallelEvaluate[SetDirectory[\$UserDocumentsDirectory]];
ParallelMap[
Export[
StringTemplate["image.jpg"]@#,
ListDensityPlot[
Import[
StringTemplate["ExampleGraphix.h5"]@#, {"Datasets", "/n1"}],
ColorFunction -> "Rainbow"
]
] &,
Range@100]


Hope this helps.