I have data in file.txt which can be expressed by 20000x11 matrix. I do not want to import all of these data. How can I plot graph of the column 1 & column 4?
3 Answers
If the data are comma-delimited and saved as myfile.txt
then
data =Import["C:\\Users\\md\\Desktop\\myfile.txt", {"Data", {All}, {1, 3}}];
imports columns 1 and 3, giving:
(*{{"a1", "a3"}, {"b1", "b3"}, {"c1", "c3"}, {"d1", "d3"}}*)
For comparison,
Import["C:\\Users\\md\\Desktop\\myfile.txt", {"Data"}]
gives
(*{{"a1", "a2", "a3", "a4", "a5"}, {"b1", "b2", "b3", "b4",
"b5"}, {"c1", "c2", "c3", "c4", "c5"}, {"d1", "d2", "d3", "d4",
"d5"}} *)
More information may be found here ("How to Import a Spreadsheet")
-
$\begingroup$ I had forgotten about sub-elements. +1 $\endgroup$– rcollyerCommented May 23, 2012 at 0:35
-
1$\begingroup$ What if it's space delimited? Are there options or do I have to convert my data file? $\endgroup$ Commented Nov 30, 2012 at 0:11
-
1$\begingroup$ @BruceConnor Good question. As far as I can see, space delimited still works
Import["C:\\Users\\md\\Desktop\\myfileSpace.txt", {"Data", {All}, {1, 3}}]
, for example. It would be good to know if you have got it to work. $\endgroup$– user1066Commented Nov 30, 2012 at 19:33 -
-
$\begingroup$ What if I want to plot the second column in this data? how can i do that? $\endgroup$– user7500Commented May 16, 2013 at 20:22
If the file is very large and importing it takes too much memory, you can use external tools to cut the columns you are interested in. You can often call these tools directly from Mathematica, for example
ReadList["!awk '{print $1, $4}' file.txt", {Number, Number}]
This will use awk to cut columns 1 and 4. ReadList
, with an explicit specification of the type of each column, is typically going to be faster and more memory efficient than Import
.
This approach can be significantly faster than Import["file.txt", "Table"][[All, {1,4}]]
if the file is large enough, and especially if it contains heterogeneous data which Import
must auto-detect.
-
$\begingroup$ Assuming *nix environment? Nice to see awk in use - been a while :) +1 for ! form which i didn't know about. $\endgroup$ Commented May 17, 2013 at 0:58
-
$\begingroup$ @SEngstrom Yes, assuming Unix for the
'
-quoting to work. I'm not sure how to specify an awk program on the command line on Windows---I think it should be"
-quoted. It should work on Windows too (if awk is installed and in the path). I used Windows until recently. The!
-syntax works in practically all importing functions and it's quite useful $\endgroup$– SzabolcsCommented May 17, 2013 at 1:57
data=Import["myFile","Table"][[All,{1,4}]];
ListPlot@data
-
3$\begingroup$ Unfortunately, that does not adhere to the OPs request to not import all of the data. $\endgroup$– rcollyerCommented May 22, 2012 at 17:51
-
$\begingroup$ @rcollyer I can't disagree with you :) But I sense some ambiguity in the original question. The data isn't too large, it should fit in memory with ease and may well produce the result the OP wanted. I'll happily withdraw the answer if it isn't what is required. $\endgroup$ Commented May 22, 2012 at 18:01
-
$\begingroup$ Let's see what they say. If it is on a memory constrained machine, then sure, but as you said, it is easily loadable in memory. Rough estimate, with 64 bit (8 byte) real numbers: 1.7 MB. Right. +1 $\endgroup$– rcollyerCommented May 22, 2012 at 18:17
-
$\begingroup$ @rcollyer that's what I thought, no problem, we'll leave it up to to them:) $\endgroup$ Commented May 22, 2012 at 18:30
Import
is too slow for you, you can try setting up a custom importer function similarly to how it was done here. $\endgroup$