# Variables for multiple data files plotting using same Mathematica notebook

I am not an expert in mathematica,

I have this following code. As you see I import a text file under the name "MyPoint1", plot with "pAll" then I do some more calculation with more variables and plots.

MyPoints = Import["/Users/Erf/Documents/points_results/test1_10.txt", "Data"];

MyPoints1 = Table[{MyPoints[[i]][[1]], MyPoints[[i]][[2]]}, {i, 1, Length[MyPoints]}];

pAll = ListPlot[MyPoints1, AspectRatio -> Automatic,
PlotStyle -> PointSize[0.0051], PlotRange -> {{0, 1200}, {0, 700}}]

nebs1[i_] :=(
list1[i] = {};
Do[distX = Abs[MyPoints1[[i]][[1]] - MyPoints1[[j]]][[1]];
distY = Abs[MyPoints1[[i]][[2]] - MyPoints1[[j]][[2]]];
If[Sqrt[distX^2 + distY^2] < 12,AppendTo[listy[i], j],Continue],{j, 1, i - 1)];
Do[distX = Abs[MyPoints1[[i]][[1]] - MyPoints1[[j]]][[1]];
distY = Abs[MyPoints1[[i]][[2]] - MyPoints1[[j]][[2]]];
If[Sqrt[distX^2 + distY^2] < 12,AppendTo[listy[i], j], Continue],{j, i + 1, Length[MyPoints]}];

pP[i_] :=
ListPlot[{blob2[[i]]}, AspectRatio -> Automatic,
PlotStyle -> Directive[PointSize[0.1], Black],
PlotRange -> {{1, 100}, {1, 100}}]

pN[i_] :=
ListPlot[Table[MyPoints1[[list1[i][[j]]]], {j, 1, Length[list1[i]]}],
AspectRatio -> Automatic,
PlotStyle -> Directive[PointSize[0.1], Red],
PlotRange -> {{1, 100}, {1, 100}}]
pN2[i_] :=
ListPlot[Table[MyPoints1[[list1[i][[j]]]], {j, 1, Length[list1[i]]}],
AspectRatio -> Automatic,
PlotStyle -> Directive[PointSize[0.1], Red],
PlotRange -> {{MyPoints1[[i]][[1]] - 20,
MyPoints1[[i]][[1]] + 20}, {MyPoints1[[i]][[2]] - 20,
MyPoints1[[i]][[2]] + 20}}]

Table[Show[pN2[ii], pP[ii]], {ii, 371, 373}]
.
.
.
.



If i have 10 or 20 data files and want to plot them all in the same mathematica notebook, every time I have to rename all the variables (i.e. Mypoints, pAll, list1, etc.) So I want to know if there is an easy way to define variables such that I don't have to rename them?

I can restart the kernel and all the variable are gonna be new for a new text file, but then my plots are not gonna be saved and I cannot change them. The other way I tried is to Find/Replace the variable, but that again is tedious. Maybe there is a way to loop everything, but again I am not an expert.

Any help and guidance is appreciated. Thank you

• You can set all your variables as functions of data file, i.e. MyPoints1[file_?String]:=Import[file, "Data"][[All,{1,2}]]; pAll[file_?String]:= ListPlot[MyPoints1[file], ...] etc. Then you get any kind of plot for particular data file, just insert its name with path as an argument. If all your files are in the same directory you can SetDirectory["/Users/Erf/Documents/points_results"] and then use short names for files as MyPoints1["test1_10.txt"]. Other possible way (without SetDirectory) is MyPoints1[FileNameJoin[{"/Users/Erf/Documents/points_results", "test1_10.txt"}]]. – Alx Dec 5 '19 at 4:05
• TakeWhile should help to replace (and simply code) thoes Do functions. You can also replace the double Part like a[[i]][[j]] with just a[[i, j]]. I would recommend the function construction tutorial as well as scoping constructs like Module and With to clean up your code. Once you have well organised code it is easier to see how to generalise it for runs over multiple sets. – Edmund Dec 5 '19 at 11:26
• This post doesn't provide the information needed to be able to experiment with your code. Too many undefined quantities. No example data to work with. It needs clarification of what you what to persist and for how long. That said, I suggest that you look at Module to localize the global variables that are troubling you. If you make such a module the body of a function that takes a file name as its argument and doest the plotting, you will not have variable conflicts when you load a new data set. Module will do the renaming. – m_goldberg Jan 22 '20 at 14:36