Question

I'm currently working on handling some experimental data with Mathematica.

I have several files that I basically want to do the same thing with. At the moment, I just copy and paste everything and change the filenames, which looks like this:

rechn1 = Import[
   "/path/4He.H2O.MeV14460.ddd.gd.dat", "Table"];
r1 = Length[rechn1];
rechn1a = Take[rechn1, {9, r1}];
plotr1 = ListPlot[rechn1a]

rechn2 = Import[
   "/path/4He.H2O.MeV15000.ddd.gd.dat", "Table"];
r2 = Length[rechn2];
rechn2a = Take[rechn2, {9, r2}];
plotr2 = ListPlot[rechn2a]

rechn3 = Import[
   "/path/4He.H2O.MeV15670.ddd.gd.dat", "Table"];
r3 = Length[rechn3];
rechn3a = Take[rechn3, {9, r3}];
plotr3 = ListPlot[rechn3a]

and so on. I'm not especially well versed in Mathematica, so this to me seemed like a good option. Copy, paste, change a few numbers.

However, I now have to handle even more data like this. So, is there any way to tell Mathematica to handle all files a certain way? And maybe also to change just a few details? For example, handle everything the same, but have a different PlotStyle or title for the plots?

It would be great to find a way to automate this process a bit, since I have a lot of files to handle now and copy and pasting just isn't an option any more.

Thanks!

Was it helpful?

Solution

As usual with Mathematica there is a variety of ways to do what you want. Here are some pieces you may be able to assemble into a solution which works for you.

You could replace your sequence of 4 function invocations with a composition of them all, like this:

ListPlot[Drop[Import["/path/4He.H2O.MeV15670.ddd.gd.dat", "Table"], 9]]

This is initially a lot harder to interpret (for you, Mathematica won't have a problem) but it will be easier to use in what follows, and it will avoid cluttering up your workspace with unnecessary temporary variables. Note that I've replaced your Take[expr,{9,Length[expr]}] with the equivalent Drop[expr,8] to avoid having to call Length on the imported data file.

Now we can tidy this up into a function, such as:

makePlot[fn_String] := ListPlot[Drop[Import[fn, "Table"], 8]]

Next, lets get all the filenames you need. I suppose that your current working directory is the parent of "/path" and that the call

hefiles = FileNames["4He.*.ddd.gd.dat", {"*"}, Infinity];

will return a list of the names of all your input files. Let's modify the makePlot function for reasons which will become obvious in a moment:

makePlot[ix_Integer, fn_String] := myPlots[ix] = ListPlot[Drop[Import[fn, "Table"], 9]];

Now, you can write

Do[makePlot[i,heFiles[[i]]],{i,1,Length[heFiles}]

and this ought, if I've got the syntax right and matched up all the brackets, to make all your plots with one statement. They will now be stored as myPlots[1], myPlots[2], ....

If you want to set options for your ListPlot command simply amend the definition of makePlot to something like

makePlot[fn_String] := ListPlot[Drop[Import[fn, "Table"], 8],plotOption-> setting, plotOption2->setting2]

or, if you want to make leaps and bounds in your Mathematica knowledge, investigate the Options function and you can modify makePlot to take options as (optional) arguments and pass them through to ListPlot.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top