# Import multiple data files in same Mathematica notebook

**URL:** <https://community.wolfram.com/t/import-multiple-data-files-in-same-mathematica-notebook/15284>\
**Category:** Questions\
**Tags:** mathematica, import-and-export\
**Created:** [January 22, 2020, 3:36pm UTC](https://community.wolfram.com/t/import-multiple-data-files-in-same-mathematica-notebook/15284 "2020-01-22T15:36:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![erfaanmoradi](https://avatars.discourse-cdn.com/v4/letter/e/9fc348/32.png) [@erfaanmoradi](https://community.wolfram.com/u/erfaanmoradi)\
**Post date:** [January 22, 2020, 3:36pm UTC](https://community.wolfram.com/t/import-multiple-data-files-in-same-mathematica-notebook/15284/1 "2020-01-22T15:36:19Z")

</div>

Greetings forum,  
I am not an expert in Mathematica, but I managed to learn by myself recently,  
My code and 2 data files are attached. The problem that I have is that I have multiple data files. I want to import all of them and be able to analyze and visualize them in the same notebook. I have problem defining variables, in my notebook you can see that I am importing 1st text file as “MyPoints400” then I can define some variables and plot them. But then when I want to do the same procedure for the next text file, I have to rename all the variables because they are already in use. Now for the next data file (x\_y\_points300.txt) I have to change “MyPoints1” to “MyPoints2” , “pAll” to “pAll2” , etc.  
Can you help me clean my code in such a way that I first import all my text files and then have the set of variables so I don’t have to copy / paste and rename them for each data file.  
Thank you in advance.

[For\_forum\_question.nb](https://community.wolfram.com/uploads/short-url/ge0naGAbPy5aKxna9aFzLQoOnpM.nb) (35 KB)  
[x\_y\_points300.txt](https://community.wolfram.com/uploads/short-url/pSatYKafBG0QcpUFTJi1YzYgoIy.txt) (3.76 KB)  
[x\_y\_points400.txt](https://community.wolfram.com/uploads/short-url/58pqNNJISn9wEikcqgdKyPRH1Ca.txt) (5.78 KB)

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [January 22, 2020, 3:44pm UTC](https://community.wolfram.com/t/import-multiple-data-files-in-same-mathematica-notebook/15284/2 "2020-01-22T15:44:50Z")

</div>

Hi Caio,

If the analysis is identical for all of the files then create a function that takes the file name as an argument and performs the analysis. No need for separate names since the same name is used in the function and scoped using `Module`.

---

<div class="post-metadata">

**Author:** ![erfaanmoradi](https://avatars.discourse-cdn.com/v4/letter/e/9fc348/32.png) [@erfaanmoradi](https://community.wolfram.com/u/erfaanmoradi)\
**Post date:** [January 22, 2020, 7:06pm UTC](https://community.wolfram.com/t/import-multiple-data-files-in-same-mathematica-notebook/15284/3 "2020-01-22T19:06:01Z")

</div>

Thanks for reply, can you please give an example how ?

---

<div class="post-metadata">

**Author:** ![murray](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/murray/32/39879_2.png) [@murray](https://community.wolfram.com/u/murray)\
**Post date:** [January 23, 2020, 1:44am UTC](https://community.wolfram.com/t/import-multiple-data-files-in-same-mathematica-notebook/15284/4 "2020-01-23T01:44:18Z")

</div>

Not a answer for you yet, but it took me a while to discern what your FORTRAN-ish (and MATLAB-ish) code for `nebs1` is doing. I think it’s simply the following:

```
data = Import["~/Downloads/x_y_points400.txt", "Data"]; (* where I downloaded the data file *)
vecMatDist[vec_?VectorQ, mat_?MatrixQ] := EuclideanDistance[vec, #] & /@ mat
nebs[i_] := 
  Flatten@Position[vecMatDist[data[[i]], Delete[data, i]], _?(# < 12 &)]
list1 = nebs /@ Range[Length@data]

```

After importing the data, this code selects, for each `i`, all those indices `j` other than `i` for which the Euclidean distance from the `i`th data point to the `j`th data point — the square-root of the sum of the squares of the differences of their two coordinates — is less than 12.

_ **Is that what you intended to do?** _ (I ask because the result is somewhat different from what you found!)

If it’s what was intended, mow make the two functions I defined take an additional argument, namely, `data`, and make the constant list `list1` (or a better name) a function of `data` as well. Then similarly for the remaining functions.

---

<div class="post-metadata">

**Author:** ![erfaanmoradi](https://avatars.discourse-cdn.com/v4/letter/e/9fc348/32.png) [@erfaanmoradi](https://community.wolfram.com/u/erfaanmoradi)\
**Post date:** [January 30, 2020, 4:24pm UTC](https://community.wolfram.com/t/import-multiple-data-files-in-same-mathematica-notebook/15284/5 "2020-01-30T16:24:38Z")

</div>

Thanks a lot for your reply. This doesn’t really help me at this stage, but I like that you simplified it in Mathematica friendly syntax.  
Well what I am trying to do is that within this notebook, I need to import 20 or more .txt files. In my notebook I showed an example with 2 files (.txt). As you see , it is not automated, meaning I have to write the same line of code for each line but different names for files and lists etc.. I want to automate the notebook such that I don’t have to do the same thing over and over for 20+ data files.  
Can you please help automate my code?

---

<div class="post-metadata">

**Author:** ![neils](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/neils/32/20595_2.png) [@neils](https://community.wolfram.com/u/neils)\
**Post date:** [January 30, 2020, 5:07pm UTC](https://community.wolfram.com/t/import-multiple-data-files-in-same-mathematica-notebook/15284/6 "2020-01-30T17:07:36Z")

</div>

Caio,

How about

```
files = FileNames["/Users/CM/Desktop/*.txt"]
datasets = Map[Import[#, "Data"] &, files]

```

Now you will have a list of all your dataset in “datasets”. You can map your analysis on the datasets, etc.

Regards,

Neil

---

<div class="post-metadata">

**Author:** ![murray](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/murray/32/39879_2.png) [@murray](https://community.wolfram.com/u/murray)\
**Post date:** [January 30, 2020, 5:52pm UTC](https://community.wolfram.com/t/import-multiple-data-files-in-same-mathematica-notebook/15284/7 "2020-01-30T17:52:14Z")

</div>

And, as I suggested before, give each function as an additional argument the dataset. For example, using the `files` and `datasets` given in Neil’s reply:

```
files = FileNames["~/Downloads/Moto_data/*.txt"] (* change as needed *)
datasets = Map[Import[#, "Data"] &, files];

vecMatDist[vec_?VectorQ, mat_?MatrixQ] := EuclideanDistance[vec, #] & /@ mat

nebs[i_, xy_] := Flatten@Position[vecMatDist[xy[[i]], Delete[xy, i]], _?(# < 12 &)]
lis[xy_] := nebs[#, xy] & /@ Range[Length@xy]

plotAll[xy_] := ListPlot[xy, AspectRatio -> Automatic, 
  PlotStyle -> PointSize[0.0051], PlotRange -> {{0, 1200}, {0, 700}}]

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

plotN2[i_, xy_] := ListPlot[Table[xy[[lis[xy][i][[j]]]], {j, 1, Length[lis[xy][i]]}], 
  AspectRatio -> Automatic, 
  PlotStyle -> Directive[PointSize[0.1], Red], 
  PlotRange -> {{lis[xy][[i]][[1]] - 20, lis[xy][[i]][[1]] + 20}, {lis[xy][[i]][[2]] - 20, lis[xy][[i]][[2]] + 20}}]

display[xy_] := (Print[plotAll[xy]]; 
  Table[Show[plotN2[ii, xy], plotP[ii, xy]], {ii, 371, 374}])

display /@ datasets

```

_ **Caution:** _ This will fail because one of the data sets is too small to have 371 through 374th items!

Even for the longer data set, either the original plotting code or my implementation of `nebs`, `lis`, `plotP`, or `plotN2` is defective, as I see no points in the plots with `plotP`,and `plotN2`. Which is why I asked if my code for `nebs` and `lis` is faithfully implementing your intent.
