S01E08: Use clipboard for table transfer by Stéphanie LESCARRET

Uitables allow the user to report and edit data easily in the same way as in a spreadsheet. They are used a lot in applications to view data in table format. Like many others, I still have the vain hope of being able to transfer data between my app and Excel and vice versa by simply copy-pasting. Unfortunately, this is not possible natively, but something could be developed rapidly to meet this need.

To develop this feature, we will need:

  • a pop-up menu, linked to a table, and 2 copy-paste actions (easy to implement with App Designer, from the R2020a version)
  • a data transfer function to and from the clipboard (an old, useful function: clipboard)

First, the design

The first step is to create a new App, and add a table:

Fill in the table with the startupFcn function:

The second step is to add a pop-up menu, and 2 copy-paste controls

Then, the programme

The clipboard function makes it possible to transfer a character string between Matlab and the clipboard. An Excel table is copied in a string, the columns of which are separated by tabulations and the rows by a line break. Functions to transform our table into a correctly formatted string must then be developed.

Copy: in the jungle of functions for handling strings, I picked sprintf function to modify a row in the table. This low-level function enables you to manage any type of data and insert tabulations easily. An instruction to translate a row: Name (=string)/Min(=numerical)/Mean(=numerical)/Max(=numerical) should be provided as follows:

str = sprintf(‘%s\t%f\t%f\t%f’, name, min, mean, max);

Then, to interjoin each row, a high-level function is used, strjoin, with line breaks as separators. Finally, the following programme is obtained to copy a table to the clipboard×263.png

Here it is:

Paste: to interpret the character string from Excel, the textscan function is used, which as sprint allows the choice of decoding format. However, be careful, as for all user actions, a verification of compatibility with the data table for what is to be pasted must be performed.

If a problem is identified in the interpreted data, an alert is sent to the user×355.png

Then we have:

To go further, the programme can be expanded to be able to copy-paste a row or a column to achieve this operation:

Share this post: