1
$\begingroup$

I have a lot of experimental datafiles I would like to plot first and check, if they can be used for further analysis or not. To avoid importing every single file I used FileNames and than mapping. This creates a plot grid, works fine so far.

I would now like to label each of the plots by the FileBaseName for quick identification. Is this possible?

Here is what I tried so far:

files = FileNames["*.dat", {"path_to_data_directory"}];
data = Import /@ files;

ListLinePlot[#, 
  InterpolationOrder -> 0, 
  PlotLabel -> FileBaseName[#] & /@ files] & /@ data

As I said, the plotting works perfect, but PlotLabel gives labeling of the plots with every single plot is labeled by the base name of the first file.

Any suggestions?

$\endgroup$
  • $\begingroup$ Take a closer look at what you are doing f[#, opt -> FileBaseName[#] & /@ {file1, file2}] & /@ {data1, data2}. Then, take a look at MapThread. $\endgroup$ – Kuba Feb 22 '16 at 17:53
  • $\begingroup$ In this case you could just map over the file names ListLinePlot[Import[#], InterpolationOrder -> 0, PlotLabel -> FileBaseName[#] ] & /@ files. (Unless of course you want to keep all the data in memory for other purpose.) $\endgroup$ – george2079 Feb 22 '16 at 18:24
  • $\begingroup$ @george2079: This works perfect. The data is still in memory and can be used with the second line of my code. Thank you so much! $\endgroup$ – sammyGreek Feb 22 '16 at 18:34
  • $\begingroup$ Nevertheless Ill give it a try to the suggestion of @Kuba $\endgroup$ – sammyGreek Feb 22 '16 at 18:34
  • $\begingroup$ my point is you don't really want to Import every file twice, although if the performance is acceptable there is no real harm in it. Another tack on this is when you import do data={#,Import[#]}&/@files , then you are carrying around a list with the data and the associated name together. $\endgroup$ – george2079 Feb 22 '16 at 18:43
1
$\begingroup$

Let's take kuba's suggestion and make it nice and tidy and name-space non-polluting.

Module[{plotF, files, names, data},
  plotF = ListLinePlot[#1, PlotLabel -> #2, InterpolationOrder -> 0] &;
  files = FileNames["*.dat", {"path_to_data_directory"}];
  names = FileBaseName[#] & /@ files;
  data = Import /@ files;
  MapThread[plotF, {data, names}]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.