3
$\begingroup$

I corrected my code but still, I can't plot my data...It was working well but when I added a 4th or 5th files...it doesn't work...I don't understande why.. even if I corrected the last line as follow

Show[func /@ {{file1, Blue}, {file2, Orange}, {file3, Purple}}, 
     PlotRange -> All] 

to Show[func /@ {{file1, Blue}, {file2, Orange}, {file3, Purple},{file4, Black}}, PlotRange -> All]

SetDirectory[NotebookDirectory[]];
(*Take all the file in the main folder where the notebook is saved*)

allFiles = FileNames["*.dat", NotebookDirectory[]];

(*function who will be used to transform the data*)

func[{file_String /; FileExistsQ[file], plotStyle_}] := 
 Module[{data, dataT, formatted}, data = Import[file, "Table"];
  dataT = Transpose[data];
  dataT = {dataT[[1]], dataT[[2]], dataT[[3]], dataT[[4]]};
  data = Transpose[dataT];
  formatted = {{#1, #2}, ErrorBar[#4, #3]} & @@@ data;
  ErrorListPlot[{formatted}, PlotStyle -> plotStyle, 
   PlotLegends -> 
    Placed[LineLegend[{FileBaseName[file]}], {Right, Top}]]]

(*Plot a combine graph of the multiple data file*)

Show[func /@ {{file1, Blue}, {file2, Orange}, {file3, Purple}}, 
 PlotRange -> All]
$\endgroup$
  • 2
    $\begingroup$ Lookup Table. $\endgroup$ – Kuba Jun 11 '18 at 10:40
  • $\begingroup$ Thanks, But I don't understand how to use Table on each line on my code... $\endgroup$ – Bigprophete Jun 11 '18 at 11:32
  • $\begingroup$ More or less Show @ Table[ ..., {file, {file1, file2, file3}}]. $\endgroup$ – Kuba Jun 11 '18 at 11:33
  • $\begingroup$ So I use the following line to import all my file, but then how to use Table to for example do the transpose of each file ? SetDirectory[NotebookDirectory[]]; allFiles = FileNames["*.dat", NotebookDirectory[]]; data = Import[#] & /@ files; $\endgroup$ – Bigprophete Jun 11 '18 at 12:08
6
$\begingroup$

Create a function that takes a filename and returns a plot. It is good practice to put all variables you use into a local Module

func[file_String /; FileExistsQ[file]] := Module[
  {
   data, dataT, formatted
   },
  data = Import[file, "Table"];
  dataT = Transpose[data];
  dataT = {dataT[[1]], dataT[[2]], dataT[[3]], dataT[[4]]};
  data = Transpose[dataT];
  formatted = {{#1, #2}, ErrorBar[#4, #3]} & @@@ data;
  ErrorListPlot[{formatted}, PlotStyle -> Red, 
   PlotLegends -> Placed[LineLegend[{FileBaseName[file]}], {0.7, 0.8}]]
  ]

Now, you can simply do

func /@ {file1, file2, file3}

and you get a list of graphics.

Edit

If you want to define the styling for each file, you can include this in the function

func[{file_String /; FileExistsQ[file], plotStyle_}] := Module[
  {
   data, dataT, formatted
   },
  data = Import[file, "Table"];
  dataT = Transpose[data];
  dataT = {dataT[[1]], dataT[[2]], dataT[[3]], dataT[[4]]};
  data = Transpose[dataT];
  formatted = {{#1, #2}, ErrorBar[#4, #3]} & @@@ data;
  ErrorListPlot[{formatted}, PlotStyle -> plotStyle, 
   PlotLegends -> Placed[LineLegend[{FileBaseName[file]}], {0.7, 0.8}]]
  ]

and then call

func /@ {{file1, Red}, {file2, Green}, {file3, Blue}}
| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank and thanks for the advise, I will try to learn better how to use it. $\endgroup$ – Bigprophete Jun 11 '18 at 12:37
  • $\begingroup$ If I want to plot them in different color, how can I do that using Colorfunction instead of PlotStyle ? $\endgroup$ – Bigprophete Jun 11 '18 at 14:05
  • $\begingroup$ I'm not sure this is possible. ErrorListPlot is basically a ListPlot, but the ColorFunction does only work for list-plots that have Joined -> True which makes absolutely no sense for an error plot. $\endgroup$ – halirutan Jun 11 '18 at 14:14
  • $\begingroup$ Ok, I used Show[func /@ files, PlotRange -> All] to generate a combine graph. how can I generate differente colore for each file in my combine file ? $\endgroup$ – Bigprophete Jun 11 '18 at 14:30
  • $\begingroup$ Please see my edit in the answer. $\endgroup$ – halirutan Jun 11 '18 at 14:37

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.