2
$\begingroup$

I had selected a matrix in a PDF file,

Mathematica graphics

and paste it onto my MMA notebook. Below is the exact copy-pasted content:

0.9034 "NAN" 0.7163 0.8588
0.3031 0.5827 0.2699 0.8063
0.0418 0.8426 "NAN" 0.8634
"NAN" 0.8913 0.0662 0.8432

I want to display this content in the matrix form as shown in the original PDF. Thankfully there was no distortion when the content was transferred; however, the white spaces now became implied multiple signs in MMA.

Mathematica graphics


My solution

1) Convert content to string by simply wrapping it in ""

2) I then ran into the problem of the "NAN"'s becoming apart from the overall string due to its being wrapped by a pair of "" previously. I couldn't find a quick way to assimilate them back into the larger string but to manually modify them (by adding a pair of \'s such that "NAN" became \"NAN\": ref)

Mathematica graphics

Mathematica graphics

Mathematica graphics

3) Use StringSplit twice, first to split the string at each line break "\n" to create 4 "lines"/sublists, and then map it the second time to each line at the "," to create separate elements for each line.

StringSplit[#, {" "}] & /@ StringSplit[%, "\n"]
(* {{"0.9034", "\"NAN\"", "0.7163", "0.8588"}, {"0.3031", "0.5827", 
  "0.2699", "0.8063"}, {"0.0418", "0.8426", "\"NAN\"", 
  "0.8634"}, {"\"NAN\"", "0.8913", "0.0662", "0.8432"}} *)

4) Replace each "\"NAN\"" with a simple "NAN"

% /. "\"NAN\"" :> "NAN"
(* {{"0.9034", "NAN", "0.7163", "0.8588"}, {"0.3031", "0.5827", "0.2699",
   "0.8063"}, {"0.0418", "0.8426", "NAN", "0.8634"}, {"NAN", "0.8913",
   "0.0662", "0.8432"}} *)

Sidestep: I tried to do this step more generally (since the string enclosed by \" might be more varied in future cases), by doing this:

% /. "\""~~x___~~"\"" :> x

This did not work, probably because ReplaceAll couldn't handle more complex string replacements (e.g. involving blanks). As a result, I tried StringReplace, and it worked as expected:

Map[StringReplace[#, "\"" ~~ x___ ~~ "\"" :> x] &, %, {2}]
(* {{"0.9034", "NAN", "0.7163", "0.8588"}, {"0.3031", "0.5827", "0.2699",
   "0.8063"}, {"0.0418", "0.8426", "NAN", "0.8634"}, {"NAN", "0.8913",
   "0.0662", "0.8432"}} *)

One-step summary

Map[StringReplace[#, "\"" ~~ x___ ~~ "\"" :> x] &, 
  StringSplit[#, {" "}] & /@ StringSplit["0.9034 \"NAN\" 0.7163 0.8588
    0.3031 0.5827 0.2699 0.8063
    0.0418 0.8426 \"NAN\" 0.8634
    \"NAN\" 0.8913 0.0662 0.8432", "\n"], {2}] // MatrixForm

Mathematica graphics


My question is:

Could anyone help me think of a more elegant/efficient way to get the same result? The greatest bottleneck, I think, is the manual adding of \'s, so I'd love to hear how you can do that programmatically.

$\endgroup$
1
  • 1
    $\begingroup$ I believe this question is a duplicate. Some options and/or related: (8997), (21951), (41280). My preferred method is still Szabolcs's Paste Tabular palette. $\endgroup$
    – Mr.Wizard
    Commented Jul 22, 2014 at 19:13

1 Answer 1

5
$\begingroup$

ImportString is all you need, as demonstrated below:

Insert copied matrix.

$\endgroup$
6
  • 2
    $\begingroup$ I hate you so much >:[ +1 $\endgroup$ Commented Jul 22, 2014 at 18:30
  • 1
    $\begingroup$ and BTW my life would have been much easier had I chosen to type in a " first before pasting the content from the PDF (in order to prompt MMA to process it). $\endgroup$ Commented Jul 22, 2014 at 18:32
  • $\begingroup$ @seismatica Haha, yep. $\endgroup$
    – C. E.
    Commented Jul 22, 2014 at 18:32
  • $\begingroup$ Nice. I've seen videos on here before. How do you make them? $\endgroup$ Commented Jul 22, 2014 at 23:11
  • $\begingroup$ @MikeHoneychurch I use LICEcap, I think most do. There are other alternatives discussed here. $\endgroup$
    – C. E.
    Commented Jul 22, 2014 at 23:19

Your Answer

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

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