0
$\begingroup$

I have a .txt file. In this file, there are four columns where the data of the first and the last column are important for me. In fact, I want to plot the last column versus the first one (density versus radius). How can I do it with Mathematica?

Here is a little part of it:

0.0 0.00149641273911    1.0 0.00148351904051
0.05    0.00149641273911    1.0 0.00148351904051
0.1 0.00299053929892    2.0 0.00148232332057
0.15    0.00598107859762    4.0 0.00148232332057
0.2 0.0089667320496 6.0 0.0014815018355
0.25    0.00895899924806    6.0 0.00148026219129
0.3 0.0178884006853 12.0    0.00147779468805
0.35    0.0149055941882 10.0    0.00147763274807
0.4 0.00892134647063    6.0 0.00147358477342
0.45    0.0237816339041 16.0    0.00147266475701
0.5 0.0267197078908 18.0    0.00147046950329
0.55    0.0133373226818 9.0 0.00146804990797
0.6 0.019229311372  13.0    0.00146396790859
0.65    0.0266098075851 18.0    0.00146274986023
0.7 0.0147447671119 10.0    0.0014582820985
0.75    0.0220696757713 15.0    0.00145453550223
0.8 0.0279204483924 19.0    0.0014522885186
0.85    0.0219620881693 15.0    0.00144688290853
0.9 0.0305841793211 21.0    0.00144076172379
0.95    0.0261777601237 18.0    0.00143840316003
1.0 0.0159427605594 11.0    0.0014320712915
1.05    0.0388780680901 27.0    0.00142473696791
1.1 0.0373336347174 26.0    0.0014209691104
1.15    0.0157011916107 11.0    0.0014124551978
1.2 0.0326389667079 23.0    0.00140594862015
1.25    0.0424536264102 30.0    0.00140142104673
1.3 0.0239562027908 17.0    0.00139390835155
1.35    0.0322186571433 23.0    0.00138511993195
1.4 0.0391244630839 28.0    0.00138178132691
1.45    0.0263499097266 19.0    0.00137240383193
$\endgroup$
2
  • 1
    $\begingroup$ Your question may be put on-hold as it seems to be off-topic, i.e it is easily found in the documentation. Don't be discouraged by that cleaning-up policy. Your future good questions are welcome. Learn about common pitfalls here. $\endgroup$
    – rhermans
    Jun 21, 2018 at 17:32
  • $\begingroup$ What is the column separator in your text file? $\endgroup$
    – m_goldberg
    Jun 21, 2018 at 18:26

3 Answers 3

2
$\begingroup$

Assuming the data.txt file is in the same folder of the MMA notebook:

data=Import[NotebookDirectory[] <> "data.txt", "Data"][[;; , {1, 4}]]

ListPlot[data]

EDIT:

if you want to multiply each column by a number "n" and "m", see code below:

Transpose[{data[[;; , 1]]*n, data[[;; , 2]]*m}]

There are many other ways of doing it, some faster than others: for more details see this link: Multiplying x and y coordinates with different scalars?

$\endgroup$
6
  • $\begingroup$ Dear @Fraccalo, what does this error means (Import::chtype: First argument $Failed<>rho20.txt is not a valid file, directory, or URL specification.) $\endgroup$ Jun 21, 2018 at 17:21
  • $\begingroup$ It looks like MMA can't find NotebookDirectory[], it may be because you haven't saved the notebook. I suggest you to save the nb in a folder with the data file and check if it works. If so, you can then change the location of the file and use the correct paths in case you need it. $\endgroup$
    – Fraccalo
    Jun 21, 2018 at 17:26
  • 2
    $\begingroup$ I think the reference to NotebookDirectory[] is unnecessary. There is no guaranties that the notebook is saved or in the same directory as the data file. $\endgroup$
    – rhermans
    Jun 21, 2018 at 17:30
  • $\begingroup$ Dear @Fraccalo, It is Ok. Could you please tell me how to multiply a number to each column? They are some scaled data. $\endgroup$ Jun 21, 2018 at 17:31
  • 1
    $\begingroup$ @PerfectFluid see edit $\endgroup$
    – Fraccalo
    Jun 21, 2018 at 17:35
3
$\begingroup$

With a default Import path:

import = Import["data.txt", "Table"];
ListPlot[import[[All, {1, 4}]]]

enter image description here

$\endgroup$
1
$\begingroup$
file="https://pastebin.com/raw/mT6u7ZkN"

In your case you put the path to the file you have, instead of the URL, for example, file="data.txt" or, just replace the file name as a string inside Import

ListPlot[
 Import[file, "Table"][[All, {1, -1}]]
 , PlotTheme -> "Scientific"
 ]

Mathematica graphics

$\endgroup$

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