File Exchange

image thumbnail

Straightforward COPY and PASTE functions

version 1.13 (5.68 KB) by

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



View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

Matlab's CLIPBOARD command allows the user to import and export data to and from other applications. But this command 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.

Comments and Ratings (40)

Jingjing Meng

very good functions


VIVEK (view profile)

Wow! This saved me so much time! Excellent tool.

Sami Rahman

Great tool! saved my day. thanks


pietro (view profile)

It should be embedded in matlab!

I like this function, but I won't rate it because I hardly ever download from Matlab Central. I am not in a position to rate it. However, I'd like to contribute by noting that many portions of a character array may contain value 0 if those characters haven't been initialized explicitly. This will cause clipboard() to fail, and the nonzero content beyond the zeros will not be transferred. The solution is to set all the 0-value bytes to 32 (space).

A great addition to the Fileexchange!

Just a quick comment. I don't know why no one else faces the same troubles, but on my system (Matlab 2011a) the call:

fails with the following error:
??? Maximum recursion limit of 500 reached. Use
to change the limit. Be aware that exceeding your
available stack space can
crash MATLAB and/or your computer.

Error in ==> usejava

When I looked into the code with debug, I was able to identify it as the line 134:
p = clipboard('paste');
for some reason this line "calls" the "paste"-file again, leading to infinite recursion.
(although I don't know why clipboard calls a function instead of performing its "supposed" action)

Anyways: I solved the problem by simply changing the filename:
paste.m -> mypaste.m
copy.m -> mycopy.m

And in addition in "copy.m" you also need to change the line 111 to "mycopy".

After this it works like a charm. :-)


Kyle (view profile)

Yvan Lengwiler

Yvan Lengwiler (view profile)

Dear Soren. You are right. Thank you for finding this. I will update the file right now.

Soren Preus

Excellent tool!

In copy, I believe line 129 should be:


instead of:


'dec' is also undefined within that same function which must be fixed in the input argument list and at line 92 and 94.

Yvan Lengwiler

Yvan Lengwiler (view profile)

Thank you, Jiro, for finding this error. I've just uploaded a corrected version. In addition, it now also supports logical arrays.

Jiro Doke

Jiro Doke (view profile)

Hi Yvan,
Very nice entry! I think I may have found a bug. On line 205, I believe it should be

if numel(posComma) == 1 && numel(posPeriod) > 0

instead of

if numel(posComma == 1) && numel(posPeriod) > 0

Chang hsiung

Yvan Lengwiler

Yvan Lengwiler (view profile)

Thank you Chang. I should have tested. I've just uploaded a small bugfix.

Chang hsiung

>> copy([1:10])
Error: File: copy.m Line: 109 Column: 5
Illegal use of reserved keyword "else".

Yvan Lengwiler

Yvan Lengwiler (view profile)

Great. I've just submitted the revision. Thank you for this improvement.


Greg (view profile)

Here is my suggestion:

        xheaders = x.Properties.VariableNames;
        xrownames = x.Properties.RowNames;
        xdescr = {x.Properties.Description};
        xt = table2cell(x);
        if isempty(xrownames)
        if isempty(cat(2,xt{:,1}))

Most of the time, for me anyways, the description and row names are blank, and I wouldn't want it copying a blank first column, so that gets removed if they aren't there.

Yvan Lengwiler

Yvan Lengwiler (view profile)

Greg, Thank you very much for this suggestion. 'table' seems to be a feature of R2013b, which I don't have. So I need some advice from you.

I can see, I believe, why you call 'copy' recursively (copy([xheaders;xt])). The reason is that [xheaders;xt] is a cell array.

But the table structure seems to accommodate rownames as well. Also, some other properties are available (such as a description). Would the following work?

xheaders = x.Properties.VariableNames;
xrownames = x.Properties.RowNames;
xdescr = x.Properties.Description;
xt = table2cell(x);

Maybe you could test this and let me know. If that works I will update the submission accordingly.


Greg (view profile)

Modified to support copying tables. Add the following lines within the main if/else.

% *** table ***************************
    elseif isa(x,'table')

Laila h


John D'Errico

John D'Errico (view profile)

Nice. useful. Well done.

John D'Errico

John D'Errico (view profile)


arnold (view profile)

ah well, no worries, thanks anyways.
It's very useful :)

Yvan Lengwiler

Yvan Lengwiler (view profile)

Arnold. This seems to be a size constraint of the clipboard itself. Even the more basic attempt to put a 1024x1024 matrix into the clipboard, clipboard('copy',x), fails. So the problem is not in my copy function, but is deeper. I cannot address this, sorry.


arnold (view profile)

copying doesn't properly work though, can't copy a 1024x1024 double array to the clipboard
>> copy(e,',');
Error using clipboard (line 63)
Java exception occurred:
java.lang.OutOfMemoryError: Java heap space


at java.lang.StringBuffer.<init>(Unknown






Error in copy (line 77)
        clipboard('copy',s); % place
        resulting string in clipboard


arnold (view profile)

thanks, well done!
There is one file here that apparently allows to copy/paste images:

Yvan Lengwiler

Yvan Lengwiler (view profile)

Arnold, thank you for your suggestion. I have implemented three options now that allow you to handle non standard decimal separators, horizontal separators, and row separators.
Importing images is a completely different issue. A possible workaround is provided by the fact that an image is really just a three-dimensional matrix. However, these functions cannot handle such objects, so you would have to copy and paste the three colors of an RGB image separately. This is tedious, of course. Doing this more comfortably would be very convenient, but I guess this goes beyond these simple utilities. Or, maybe you have an idea how to do that?


arnold (view profile)

nice work, two remarks:

1. Switchable decimal separator
Some of us work in a windows environment that uses commas for separators (in Office i.e.). Maybe you could implement an option where one can easily switch between '.' and ',' as decimal separators. example:

this would copy the array to the clipboard with ',' as decimal separator. The standard option (copy(x)) should very well use points!

Example for paste. In Clipboard: '0,1'

--> x=0.1


--> y=1;

2. pasting Images
It would be great if you implemented pasting image-data, like a screenshot as an RGB Array.
This would be really cool, but I can't estimate how much work it'll cost, but then you'd definitely have owned your five stars ;)

Yvan Lengwiler

Yvan Lengwiler (view profile)

Thank you, Hakan. This is a useful remark. I've uploaded a revision of the 'paste' function that tries to address this issue. It is, I believe, not possible to identify the formatting of the decimal and thousand separators with 100% accuracy (given an arbitrary string). The program will succeed if a comma is present in the number string.

So, both '1'234.56' and '1.234,56' will now be translated into the number 1234.56. Yet, '1.234' will always be interpreted as the number 1.234, even if 1234 was meant.


hakan (view profile)

Very usefull, but does not recognize "," as a decimal mark (which is default mark on excel).


Rob (view profile)



Joe (view profile)


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

Yvan Lengwiler

Yvan Lengwiler (view profile)

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

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.

Yvan Lengwiler

Yvan Lengwiler (view profile)

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.

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)];
                    str = onecell(str, x{r,c}, char(9)); % treat cell, append a tab
            if isempty(x{r,end}),
                str = [str char(10)];
                str = onecell(str, x{r,end}, char(10));% treat cell, append a linefeed
            clipboard('copy',str); % copy to clipboard

Extremely useful. Well done

Jonas Lundgren

Jonas Lundgren (view profile)

Very useful! Tank you.



Copy now supports figures.


bug fix


Corrected a bug that Jiro Doke found (Thank you, Jiro). In addition, copy() now also supports logical arrays.


small bug-fix


Support for table variables (suggestion by Greg).


Three new options are implemented, with allow the handling of unusually formatted numbers or separators (following a suggestion by arnold).


The paste function now tries to identify non-conventional decimal and thousand separators.


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


small improvemnt of efficiency


copy now also handles multi-line character arrays


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

MATLAB Release
MATLAB 7.10 (R2010a)

Inspired: vjmedina/MCTB

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video