Message Boards Message Boards

0
|
6712 Views
|
6 Replies
|
5 Total Likes
View groups...
Share
Share this post:

Import multiple data files in same Mathematica notebook

Posted 4 years ago

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 (xypoints300.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.

POSTED BY: Caio Moto
6 Replies

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

POSTED BY: Neil Singer

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.

POSTED BY: Murray Eisenberg

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 ith data point to the jth 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.

POSTED BY: Murray Eisenberg
Posted 4 years ago

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?

POSTED BY: Caio Moto
Posted 4 years ago

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.

POSTED BY: Rohit Namjoshi
Posted 4 years ago

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

POSTED BY: Caio Moto
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract