Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
1  
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. –  Mr.Wizard Jul 22 at 19:13

1 Answer 1

up vote 5 down vote accepted

ImportString is all you need, as demonstrated below:

Insert copied matrix.

share|improve this answer
2  
I hate you so much >:[ +1 –  seismatica Jul 22 at 18:30
1  
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). –  seismatica Jul 22 at 18:32
    
@seismatica Haha, yep. –  Pickett Jul 22 at 18:32
    
Nice. I've seen videos on here before. How do you make them? –  Mike Honeychurch Jul 22 at 23:11
    
@MikeHoneychurch I use LICEcap, I think most do. There are other alternatives discussed here. –  Pickett Jul 22 at 23:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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