1
$\begingroup$

I've been working for a few days trying to make a dynamic histogram on Mathematica, but I can't seem to make it happen. You see, I'm fairly new to working with Mathematica and thus lack some knowledge on how it works.

First, I have a bunch of files listed in a directory as:

Step001.txt, Step002.txt, ... Step037.txt, Step038.txt, ...

Each file contains multiple hundred lines of data written as such:

x1, y1
x2, y2
...
x512, y512
x513, y513
...

I tried importing the files with the Import function:

file  = Import["/Users/.../Directory/", "*.txt"]

but it imports all the files at once without discerning them from one another.

I then tried importing them individually:

file  = Import["/Users/.../Directory/STEP001.txt"]
file  = Import["/Users/.../Directory/STEP002.txt"]
...

But I have so many files that it is not conceivable to list them all. Is there a more concise way (a loop, perhaps?) to import them all and store the data from each data set in different arrays or tables?

Second, I've tried using the Manipulate function with the Histogram3D, trying to use the parameter values as "time", for which parameter=1 is the data from Step001.txt file, parameter=2 is the data from Step002.txt etc.:

Manipulate[Histogram3D[file],{time,0,10}]

However, the Histogram that appears only shows the last file I imported at all values of parameters in a static way.

Can somebody help me? Thanks!

$\endgroup$

1 Answer 1

2
$\begingroup$

Creating five test files to play with, named "Step1.txt","Step2.txt", etc:

Do[
 Export["testfiles/Step" <> ToString[n] <> ".txt", 
  Transpose[RandomVariate[NormalDistribution[1, .5], {2, 1000}]], "CSV"],
 {n, 1, 5}]

These should resemble your files:

enter image description here

Import them all into a list called data. I usually prefer importing as comma separated values (CSV).

files = FileNames["testfiles/Step*.txt"];
data = Import[#, "CSV"] & /@ files;

Then the data from each file can be accessed with data[[n]]:

Manipulate[Histogram3D[data[[n]]], {n, 1, Length[data], 1}]

enter image description here

$\endgroup$
1
  • $\begingroup$ It's working! Thank you so much! $\endgroup$
    – Felix
    Jun 28, 2019 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.