Group Abstract Group Abstract

Message Boards Message Boards

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

Import multiple data files in same Mathematica notebook

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