3
$\begingroup$

XYScan produces results in the following form (as *.txt):

# xyscan Version 3.3.1
# Date: Wed Sep 23 11:01:55 2015
# Scanned by: dimitris
# Source: /media/dimitris/E6B09AEDB09AC409/Documents and Settings/Dimitris/Documents/Prony_Math/F10.png
# Comment: 
# Format: x y -dx +dx -dy +dy
-0.20155    2.90211 0   0   0   0   
0.268734    2.92958 0   0   0   0   
0.802326    2.98451 0   0   0   0   
1.27261 3.03028 0   0   0   0   
1.78811 3.0669  0   0   0   0   
2.28553 3.10352 0   0   0   0   
2.50258 3.14014 0   0   0   0   
2.78295 3.18592 0   0   0   0   
-0.20155    2.74648 0   0   0   0   
0.268734    2.7831  0   0   0   0   
0.784238    2.82887 0   0   0   0   
1.27261 2.8838  0   0   0   0   
1.79716 2.92042 0   0   0   0   
2.27649 2.9662  0   0   0   0   
2.49354 2.98451 0   0   0   0   
2.79199 3.02113 0   0   0   0   
# EoF

I want Mathematica to read this *.txt file and given the above data to return the list

{{{-0.20155, 2.90211}, {0.268734, 2.92958}, {0.802326, 
   2.98451}, {1.27261, 3.03028}, {1.78811, 3.0669}, {2.28553, 
   3.10352}, {2.50258, 3.14014}, {2.78295, 3.18592}}, {{-0.20155, 
   2.74648}, {0.268734, 2.7831}, {0.784238, 2.82887}, {1.27261, 
   2.8838}, {1.79716, 2.92042}, {2.27649, 2.9662}, {2.49354, 
   2.98451}, {2.79199, 3.02113}}}

That is, discard the zeros and every 8 (horizontal) pairs of data consist a sngle list; i.e. here two lists. How is it possible that?

(EDIT)

FilePrint["C:\Users\Dimitris\Documents\Prony_Math\-49.txt"] returns the file but also produces the error messages

Syntax::stresc: Unknown string escape \U.

Syntax::stresc: Unknown string escape \D.

Syntax::stresc: Unknown string escape \D.

Syntax::stresc: Unknown string escape \P.

In addition,

ReadList["C:\Users\Dimitris\Documents\Prony_Math\-49.txt", 
  Number, \!\(\*
ButtonBox["RecordLists",
BaseStyle->"Link",
ButtonData->"paclet:ref/RecordLists"]\) -> \!\(\*
ButtonBox["True",
BaseStyle->"Link",
ButtonData->"paclet:ref/True"]\)]

ReadList::intnm: Non-negative machine-sized integer expected at position 3 in ReadList[C:\Users\Dimitris\Documents\Prony_Math\-49.txt,Number,RecordLists->True]. >>

(EDIT_2) I erased the first 6 lines of the *.txt file and modified the setting of the numbers as follows

-0.20155    2.90211     0   0   0   0   
0.268734    2.92958     0   0   0   0   
0.802326    2.98451     0   0   0   0   
1.27261     3.03028     0   0   0   0   
1.78811     3.0669      0   0   0   0   
2.28553     3.10352     0   0   0   0   
2.50258     3.14014     0   0   0   0   
2.78295     3.18592     0   0   0   0   
-0.20155    2.74648     0   0   0   0   
0.268734    2.7831      0   0   0   0   
0.784238    2.82887     0   0   0   0   
1.27261     2.8838      0   0   0   0   
1.79716     2.92042     0   0   0   0   
2.27649     2.9662      0   0   0   0   
2.49354     2.98451     0   0   0   0   
2.79199     3.02113     0   0   0   0

Still, the afforementioned error messages remain.

$\endgroup$
  • $\begingroup$ Ok, thanks again. $\endgroup$ – Dimitris Sep 23 '15 at 11:15
  • 1
    $\begingroup$ The "Unknown string escape" is a warning because you are using single backslashes in your file name. They should be escaped as double backslashes, or you can use forward slashes. Fix the string, or you can do Quiet@FilePrint $\endgroup$ – george2079 Sep 23 '15 at 15:42
  • $\begingroup$ possible duplicate: mathematica.stackexchange.com/q/50718/2079 $\endgroup$ – george2079 Sep 23 '15 at 15:44
5
$\begingroup$

Your text data

str = "# xyscan Version 3.3.1
  # Date: Wed Sep 23 11:01:55 2015
  # Scanned by: dimitris
  # Source: /media/dimitris/E6B09AEDB09AC409/Documents and Settings/Dimitris/Documents/Prony_Math/F10.png
  # Comment: 
  # Format: x y -dx +dx -dy +dy
  -0.20155    2.90211 0   0   0   0   
  0.268734    2.92958 0   0   0   0   
  0.802326    2.98451 0   0   0   0   
  1.27261 3.03028 0   0   0   0   
  1.78811 3.0669  0   0   0   0   
  2.28553 3.10352 0   0   0   0   
  2.50258 3.14014 0   0   0   0   
  2.78295 3.18592 0   0   0   0   
  -0.20155    2.74648 0   0   0   0   
  0.268734    2.7831  0   0   0   0   
  0.784238    2.82887 0   0   0   0   
  1.27261 2.8838  0   0   0   0   
  1.79716 2.92042 0   0   0   0   
  2.27649 2.9662  0   0   0   0   
  2.49354 2.98451 0   0   0   0   
  2.79199 3.02113 0   0   0   0   
  # EoF";

or

str = Import["filename.txt", "Text"];

Remember to use double backslash for paths. As an example, evaluate InputForm[$UserBaseDirectory] and see how it looks.

If you know which lines to remove

Read the details for Part, Most and Rest.

data= Most@ImportString[str, "Table"][[7 ;;, {1, 2}]]

or

data = ImportString[str, "Table"][[7 ;; -2, {1, 2}]]

or

data = ImportString[str, "Table", HeaderLines -> 6][[;; -2, {1, 2}]]
{{-0.20155, 2.90211}, {0.268734, 2.92958}, {0.802326, 2.98451}, {1.27261, 3.03028}, {1.78811, 3.0669}, {2.28553, 3.10352}, {2.50258, 3.14014}, {2.78295, 3.18592}, {-0.20155, 2.74648}, {0.268734, 2.7831}, {0.784238, 2.82887}, {1.27261, 2.8838}, {1.79716, 2.92042}, {2.27649, 2.9662}, {2.49354, 2.98451}, {2.79199, 3.02113}}

Removing automatically all the lines that start with "#"

data = ImportString[
  StringTrim@
   StringDelete[str, Shortest[StartOfLine ~~ "#" ~~ ___ ~~ EndOfLine]]
  , "Table"][[All, {1, 2}]]

or

ImportString[
  StringDelete[str, 
   Shortest[StartOfLine ~~ "#" ~~ ___ ~~ EndOfLine] ~~ {"\n", "\r", "\r\n", ""}]
  , "Table"][[All, {1, 2}]]

Other people have suggested to load all the table and then select only the lines where the first cell is a number

Cases[ImportString[str, "Table"][[All,{1,2}]], {_?NumberQ, ___}];

Visually inspect your data

TableForm[data, TableHeadings -> {Range[Length[data]], None}]

Mathematica graphics

Depending how yo do it you could change ImportString[str,"Table"] for Import[filename,"Table"]

Read the options in the documentation for Table(.dat) and HeaderLines.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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