Code covered by the BSD License  

Highlights from
Straightforward COPY and PASTE functions

  • copy(x) COPY puts a string, cell array, or numerical array into the clipboard,
  • paste() PASTE copies the content of the clipboard to a variable; creates a
  • View all files

5.0

5.0 | 6 ratings Rate this file 13 Downloads (last 30 days) File Size: 4.68 KB File ID: #28016
image thumbnail

Straightforward COPY and PASTE functions

by Yvan Lengwiler

 

25 Jun 2010 (Updated 22 Jun 2011)

Allows very simple manual exchange with other applications through the clipboard.

| Watch this File

File Information
Description

Matlab's CLIPBOARD command allows the user to import and export data to and from other applications. But this command it is limited to strings, or requires some mouse work when importing numerical arrays with the 'pastespecial' option.

This submission consists of two functions that make moving simple data from and to Matlab extremely easy. They are used as follows:

1) From Matlab to other application: In Matlab, say copy(x). Then in the other program (eg Excel, Word, Mathematica etc), import with the local Paste function. Depending on what kind of class x is, different material is pushed to the clipboard. For instance, if x is a numerical array, the receiving function will also see numerical data, not a string (as it would when using clipboard('copy',...)).

2) From another application to Matlab: In the other application, use the copy function to place content into the clipboard. Then, in Matlab, say x = paste; Depending of the structure of the data in the clipboard, the program makes x either a numerical array, a cell array, or a string.

See the help provided in the two functions for some examples. The two functions are limited to numerical arrays, cell arrays, and strings. They do not support structs or special classes.

Please comment if you like it or find it useful.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
num2clip: copy numerical arrays to clipboard

MATLAB release MATLAB 7.10 (2010a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (9)
29 Jun 2010 Jonas Lundgren

Very useful! Tank you.

06 Jul 2010 Riccardo Meldolesi

Extremely useful. Well done

22 Jun 2011 Joseph Burgel

Hi Yvan. Nice ftns. One thing I changed was to add special conditions for when a cell array has an 'empty' element. Rather that copying '[]', I test for empty and then simply add the tabs/CR. This way, Excel doesn't print anything for empties. Not sure if this will screw anything else up... In copy.m, line 51:

    % *** cell argument **************************************************
    elseif isa(x,'cell')
        [nrow, ncol] = size(x);
        str = '';
        for r = 1:nrow
            for c = 1:ncol-1
                if isempty(x{r,c}),
                    str = [str char(9)];
                else
                    str = onecell(str, x{r,c}, char(9)); % treat cell, append a tab
                end
            end
            if isempty(x{r,end}),
                str = [str char(10)];
            else
                str = onecell(str, x{r,end}, char(10));% treat cell, append a linefeed
            end
            clipboard('copy',str); % copy to clipboard
        end
Thanks,

22 Jun 2011 Yvan Lengwiler

Thank you, Joseph. That's a valid point. I've addressed it (maybe slightly more elegantly) by catching this case in the 'onecell' subfunction.

function str = onecell(str,e,ch)
    if isempty(e)
        str = [str, ch];
    elseif isa(e,'char') etc...

Update should be available soon.

19 Dec 2011 Robert Alvarez

After fighting with Matlab's clipboard() function for an hour, I found these functions. They do what they're supposed to, no fuss, no bother. Thanks.

25 Dec 2011 Yvan Lengwiler

Robert, thank you for your comment. I'm glad that the functions are helpful.

24 Jan 2012 Stefano

Thank you for these functions.
Very useful to overcome the limitations of the clipboard function.

17 Feb 2012 Joe  
04 May 2012 Joachim  
Please login to add a comment or rating.
Updates
28 Jun 2010

Fixed a small bug in 'paste' and expanded help text.

10 Jul 2010

copy now also handles multi-line character arrays

05 Jun 2011

small improvemnt of efficiency

22 Jun 2011

Special treatment of empty cells (following a suggestion by Joseph Burgel).

Tag Activity for this File
Tag Applied By Date/Time
data import Yvan Lengwiler 28 Jun 2010 09:51:55
data export Yvan Lengwiler 28 Jun 2010 09:51:55
clipboard Yvan Lengwiler 28 Jun 2010 09:51:55

Contact us at files@mathworks.com