5
$\begingroup$

Is there an easy way to take a column output from a TableForm command, like this:

6.3121375196805859436
6.9003005747830988641
8.9768182443850834342
8.9138378561826490430
7.9892637649923361149
5.7310834764460301485
7.8127176986072821866
7.2290808062999679496
7.4923081278967455513

And convert it back into a "Table" or "List" or "Vector", i.e.,

    {6.3121375196805859436,6.9003005747830988641,8.9768182443850834342,8.9138378561826490430,7.9892637649923361149,5.7310834764460301485,7.8127176986072821866,7.2290808062999679496,7.4923081278967455513}

It is not so hard to do manually if there are only a dozen items, but when there are hundreds, it gets a bit tedious. What I've been doing is copy as "plain text", paste into Microsoft Word, replace "paragraph" marks with commas (Find ^p; Replace ,), put in the curly brackets, and copy it back into Mathematica. Surely there is an easier way?

Well, here is one way, and recall that I want to just translate within Mathematica itself:

StringReplace["{0.27053645617075607328
0.29665173751384943109
0.25127005359909123280
0.23465443792124858652
0.25447200323727824111
0.26802978565269905254
0.26622025533442188507
0.31359242691527905846
0.26810428054117807246
0.29408129579900499019
0.29001875144558125364
0.24823119448471783028
0.27626029400322289905
0.27347835282168678727
0.26924305884480351606
0.25995335230286428315
0.25585822835794553252
0.26573081058995874804
0.28819488915098074127
0.27186805052334818319
0.31654336155657666993}", "\n" -> ","]

That is, copy it as text, and paste it into the command

StringReplace["{paste here}", "\n" -> ","]

The output is then formatted correctly. I would have thought it would be a builtin command, and well, sort of, it is. Anyone else have an answer, please?

$\endgroup$
4
  • $\begingroup$ but when there are hundreds, it gets a bit tedious. What I've been doing is copy as "plain text", paste into Microsoft Word you are making things so hard for yourself. Simply use Latex. Mathematica has great Latex export. You can export the data from your table in Mathematica to Latex using TeXForm and include the output back to your latex document. Word is not the right tool for engineering, science and math reports. I use Latex for everything. $\endgroup$
    – Nasser
    Jul 17, 2021 at 4:50
  • $\begingroup$ Don't follow that. I copied via LaTeX, and pasted in a .txt file it gave:\left( \begin{array}{c} 0.99914480523366600774343767837101128030306037301171630246007280113 \\ 0.99886229380227662563491290721686478149534907102631923379939140936 \\ 0.99912233712195990056006452559240871933654682894782657439809033693 \\ 0.99949936790492257234374456221274757414510894431488309820117792698 \\ 0.99915451603281797293363463509619228642538870957763819953196258552 \\ \end{array} \right) $\endgroup$
    – Carl
    Jul 17, 2021 at 6:10
  • 1
    $\begingroup$ Can you copy the table to an input cell and then set a variable to equal it, then look at the FullForm of the variable, and then operate on that? $\endgroup$ Jul 17, 2021 at 16:53
  • 1
    $\begingroup$ @berniethejet Sort of. But that gave me an idea that worked. Namely an adaptation of your comment and Chris Degnen's answer, see the comment below his post. $\endgroup$
    – Carl
    Jul 18, 2021 at 9:18

4 Answers 4

5
$\begingroup$
Clear["Global`*"]

TableForm should only be used for display. Assign a name to the input to TableForm and take the column that you want from the input.

SeedRandom[1234];

(mat = RandomReal[1, {9, 3}]) // TableForm

enter image description here

For the second column

mat[[All, 2]]

{* {0.521964, 0.0116446, 0.479332, 0.984993, 0.884729, 0.91956, 0.587943, \
0.696159, 0.632741} *}

Or, for the first column

mat[[All, 1]]

(* {0.876608, 0.377913, 0.543757, 0.759896, 0.459017, 0.263973, 0.98729, \
0.790215, 0.401549} *)
$\endgroup$
2
  • 2
    $\begingroup$ Fair enough, but when the current context is dumped, all one may have is the printout of the table left of the original calculation, and if that takes 24 h to calculate, one would have the problem of translating a table into a vector whose origin is difficult to resurrect, in order to use it for further processing. That is the context I am most interested in. It is fine to say shouldn't, but without saving the context, what you suggest is not typically useful in my circumstance. In turn, saving the context is a bit tedious, I have done so in the past, but it is not time deployment efficient. $\endgroup$
    – Carl
    Jul 17, 2021 at 10:01
  • $\begingroup$ On the other hand, what is displayed in a table is actually stored at full precision, just not shown as such, so that a generic "copy," followed by generic "paste" into a blank area then wrapped in a Flatten[...] command, which when executed will resurrect the full precision vector from a Table column as shown by following @ChrisDegnen's suggestion. $\endgroup$
    – Carl
    Jul 19, 2021 at 21:33
4
$\begingroup$

Using sample data

TableForm[RandomReal[{0, 1}, {5, 3}]]

Select middle column, Copy & Paste.

Add ; (optional), and evaluate, followed by

a = Flatten@%

This should set the middle column to a with the original precision.

$\endgroup$
6
  • $\begingroup$ What's wrong with % // InputForm $\endgroup$
    – chris
    Jul 17, 2021 at 8:58
  • $\begingroup$ @chris Can do that, but the output is not then a vector. $\endgroup$ Jul 17, 2021 at 9:25
  • 1
    $\begingroup$ I like it. I don't like using the semicolon because that converts the output into machine precision. Without it, the Flatten command does not process, but at least it does translate at full precision within that command, so that it can then be copied and pasted correctly, at least sometimes. Can you do something to prevent the automatic conversion to machine precision; it is somewhat inconstant? $\endgroup$
    – Carl
    Jul 17, 2021 at 10:29
  • 1
    $\begingroup$ What I can do, is "copy" without choosing a copy format, i.e., just copy. Then paste that, wrap in Flatten[...] and run that. It then resurrects the original precision, which in my case is longer than the 20 significant figures I usually display, and does so as a vector! BTW (+1). $\endgroup$
    – Carl
    Jul 18, 2021 at 9:13
  • $\begingroup$ @Carl Yes, that works well. Copy As > Plain Text is good for Excel but not necessary here. $\endgroup$ Jul 18, 2021 at 22:24
2
$\begingroup$

<...> when the current context is dumped, all one may have is the printout of the table left of the original calculation, and if that takes 24 h to calculate, one would have the problem of translating a table into a vector whose origin is difficult to resurrect, in order to use it for further processing. That is the context I am most interested in.

If you have an output cell with TableForm output, you can create under it an input cell with the following code which will extract the contents of the TableForm from the output cell and assign it to a variable table:

table = First@
  Cases[NotebookRead[PreviousCell[]], 
   grid : TagBox[GridBox[__], _] :> ToExpression@grid, -1]

Here is how it can be used:

screenshot


Another method is to right-click on the output cell bracket and select Convert To > InputForm. The result will look as follows:

screenshot

Now you can easily select the contents of the recovered original TableForm input by double-clicking on the first curly bracket in the TableForm expression, after what you can simply copy the input matrix:

screenshot

$\endgroup$
9
  • $\begingroup$ Yes, that works, but it answers a question I didn't ask, namely how to reformat an entire table, whereas I might only want one or two vectors. (+1), anyway, at least it works. $\endgroup$
    – Carl
    Jul 19, 2021 at 21:12
  • 1
    $\begingroup$ @carl Side note: the shortcut for Convert to->**InputForm** is Ctrl+Shift+I (in Windows). For more info you may search shortcut in the document. $\endgroup$
    – xzczd
    Jul 20, 2021 at 2:59
  • $\begingroup$ @Carl Strictly speaking, as the second method I propose a conversion to InputForm rather than reformatting. You always can create a copy of the original table before the conversion. The first method allows to copy the table, you can use CopyToClipboard to automate it. $\endgroup$ Jul 20, 2021 at 3:21
  • $\begingroup$ Well, command+shift+I (Mac) on part of a table got me { {NumberForm[ 13.7954506582797826869128070985460977131329506391598692631535901910\ 5711700089517$`$63.29951679410639, 20]}, {NumberForm[ 9.51723244808496444842171494285858490371772785926750641413562067013\ 827626151444$`$62.895016813692926, 20]}, {NumberForm[ 6.05884360398624400908459908131841674266268428965725478730986628166\ 706954188622$`$62.17672870969089, 20]}, {NumberForm[ 9.96452005976523317741573978711645175548238211267411655738745691084\ 284653990601$`$62.02845034867765, 20]}} $\endgroup$
    – Carl
    Jul 20, 2021 at 4:00
  • $\begingroup$ Con't If I run that (shift+return) I get {{8.7114057567481581358},{6.3432511520078327174},{5.8909344004427558882},{13.795450658279782687},{9.5172324480849644484},{6.0588436039862440091},{9.9645200597652331774}}, which brings me back to Flatten[whatever] to reconstruct it as a vector. $\endgroup$
    – Carl
    Jul 20, 2021 at 4:06
0
$\begingroup$

You could take the column with arrayName[[3]] or arrayName[[;;,3]] (if it is column three; the ;; is Span and captures all of that dimension) and export it to a file with Export[pathAndName.xlsx,arrayname[[appropriate designation]] to open it in Excel and copy from there.

$\endgroup$
1
  • 1
    $\begingroup$ That doesn't help. I can just "copy as" choice "plain text" and paste into Excel. However, Excel only stores 20 significant figures, and when copying it, if only copies what is visible, which might be only 3 significant figures. Word works better as a work around because it doesn't interpret the characters pasted into it. $\endgroup$
    – Carl
    Jul 17, 2021 at 6:04

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.