1
$\begingroup$

Given an array I would like to print it in Mathematica such that the values can be simply copied and pasted into an Excel's row. What is the best approach for this?

data = Array[0, 32];
For[k = 1, k < 100, k++,
  i = RandomInteger[{1, 32}];

   data[[i]] = data[[i]] + 1;

  ];

data

output:

{5 + 0[1], 4 + 0[2], 4 + 0[3], 3 + 0[4], 1 + 0[5], 1 + 0[6], 1 + 0[7],
  5 + 0[8], 3 + 0[9], 0[10], 3 + 0[11], 2 + 0[12], 4 + 0[13], 
 4 + 0[14], 2 + 0[15], 5 + 0[16], 1 + 0[17], 4 + 0[18], 2 + 0[19], 
 1 + 0[20], 4 + 0[21], 2 + 0[22], 2 + 0[23], 2 + 0[24], 4 + 0[25], 
 2 + 0[26], 4 + 0[27], 4 + 0[28], 4 + 0[29], 8 + 0[30], 4 + 0[31], 
 4 + 0[32]}
$\endgroup$
  • 1
    $\begingroup$ data = Array[a, {5, 5}]; Export["exportedData.xls", data // Flatten]; Then open exportedData.xls in Excel $\endgroup$ – Bob Hanlon Oct 3 '16 at 13:04
  • 1
    $\begingroup$ @BobHanlon, thank, but I needed a way to display the Array in the Notebook in a Excel copy and paste friendly format. $\endgroup$ – Saqib Ali Oct 3 '16 at 14:36
  • 1
    $\begingroup$ data // Flatten // Column select data then Copy As Plain Text. Select cell in Excel and Paste $\endgroup$ – Bob Hanlon Oct 3 '16 at 14:52
  • $\begingroup$ @BobHanlon, that displays something { {3 + 0[1]}, {5 + 0[2]}, {12 + 0[3]}, {24 + 0[4]}, {44 + 0[5]}, {101 + 0[6]}, {163 + 0[7]}, {260 + 0[8]} } How can I make it display JUST the values, and not the Array indexes. $\endgroup$ – Saqib Ali Oct 4 '16 at 1:02
  • $\begingroup$ @BobHanlon I have updated the question with the mathematica code that i am using. $\endgroup$ – Saqib Ali Oct 4 '16 at 1:18
3
$\begingroup$
n = 10;

data = ConstantArray[0, n];

Note use of ConstantArray rather than Array

For[k = 1, k < 100, k++, i = RandomInteger[{1, n}];
  data[[i]] += 1;];

Note use of AddTo (+=)

data // Column

enter image description here

Select column in workbook then Copy As Plain Text. Select cell in Excel and Paste

$\endgroup$
5
$\begingroup$

I suggest to print the data as a "TSV" string in a separate Cell:

data = RandomReal[1, 10];
CellPrint@Cell[ExportString[{data}, "TSV"], "Print", TextClipboardType -> "PlainText"]
0.25961013187287674   0.12283879171332623 0.41817318423675176 0.7052661059327268  0.2174563190189165  0.5695344741754753  0.2714439130949191  0.4690459007786689  0.641512306420351   0.2207889610686835

Then you can select the row by double-clicking, copy and paste into an Excel sheet:

screenshot

$\endgroup$
  • 1
    $\begingroup$ CopyToClipboard@ExportString[data, "TSV"] is nice too. (and works better I think with a 2d matrix ) $\endgroup$ – george2079 Oct 4 '16 at 13:39

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.