Group Abstract Group Abstract

Message Boards Message Boards

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

Import multiple data files in same Mathematica notebook

Posted 6 years ago
6 Replies
Posted 6 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

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

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

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
Posted 6 years ago
POSTED BY: Caio Moto
Posted 6 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