4

I'm writing a MatLab programm under windows 7 x64 that takes the clipboard, with mixed data copied from Microsoft Excel, and import it directly to MatLab into a single char array.

When I use str = clipboard('paste') in MatLab, all text data copied from excel are fine, but the numerical data copied loss some decimal places.

For example, if the cell A1 contains the number 2113.12389881239, but it only displays the value 2113.123899 due to cell formating, then 2113.123899 is copied from excel to MatLab instead of the real cell value 2113.12389881239.

I would like to know how to copy the exact cell value stored in Excel not the value displayed.

Thanks in advance.

Transfinito
  • 171
  • 3
  • 13
  • Is there any reason you can't format the cells as `@`? – jmac Sep 27 '13 at 04:21
  • @jmac There is one reason. The same excel file is used for other purposes. So is not very use full change/force Excel's cells format to text, only for data copy. – Transfinito Sep 27 '13 at 05:34

1 Answers1

0

Given the following value: .1234567, formatted with all from 1-7 decimal places, I get the following results when I copy-paste to another program (notepad):

0.1 
0.12 
0.123 
0.1235 
0.12346 
0.123457 
0.1234567 

Assuming you want to copy one number at a time, I used this macro:

Sub FormattedCopy()

    Dim oldFormat, copyString As String    

    oldFormat = Selection.NumberFormatLocal

    Selection.NumberFormatLocal = "@"

    Selection.Copy

    MsgBox ("Please paste your data")

    Selection.NumberFormatLocal = oldFormat

End Sub

The message box serves to 'pause' the process, because changing the number format back will clear the clipboard. When I paste any value, it properly displays in notepad as 0.1234567

You can do this with a range of values if you save the range of number formats in an array and feed it back as I did above with for loops.

jmac
  • 7,078
  • 2
  • 29
  • 57
  • I know I'm being late but I also just struggle with clipboard decimal precision. Your answer does not help, unfortunately; this is not about the number of decimal places (7 still working in your test), but about the total number of significant places, with OP's `2113.12389881239` having 15, and the shortened `2113.123899` 10 (still more than your 7). – Marius Wallraff Oct 08 '21 at 08:20