Feed on
Posts
Comments

although matlab programmers are loath to admit it, there are circumstances during which we are forced to use excel. in such trying times, here’s a quick script that i found on matlab central for copying an array in an excel-readable format onto the clipboard. to use it, save the code below the asterisks as num2clip.m. then, when you need to move an array into excel, call in matlab:

>> num2clip(my_array)

next, open up excel, go to “edit” and click on paste. that easy!

********************************************************

here’s the code to save in num2clip:

function arraystring = num2clip(array)
%NUM2CLIP copies a numerical-array to the clipboard
%
% ARRAYSTRING = NUM2CLIP(ARRAY)
%
% Copies the numerical array ARRAY to the clipboard as a tab-separated
% string. This format is suitable for direct pasting to Excel and other
% programs.
%
% The tab-separated result is returned as ARRAYSTRING. This
% functionality has been included for completeness.
%
%Author: Grigor Browning
%Last update: 02-Sept-2005

%convert the numerical array to a string array
%note that num2str pads the output array with space characters to account
%for differing numbers of digits in each index entry
arraystring = num2str(array);
arraystring(:,end+1) = char(10); %add a carrige return to the end of each row
%reshape the array to a single line
%note that the reshape function reshape is column based so to reshape by
%rows one must use the inverse of the matrix
arraystring = reshape(arraystring’,1,prod(size(arraystring))); %reshape the array to a single line

arraystringshift = [' ',arraystring]; %create a copy of arraystring shifted right by one space character
arraystring = [arraystring,' ']; %add a space to the end of arraystring to make it the same length as arraystringshift

%now remove the additional space charaters – keeping a single space
%charater after each ‘numerical’ entry
arraystring = arraystring((double(arraystring)~=32 | double(arraystringshift)~=32) & ~(double(arraystringshift==10) & double(arraystring)==32) );

arraystring(double(arraystring)==32) = char(9); %convert the space characters to tab characters

clipboard(‘copy’,arraystring); %copy the result to the clipboard ready for pasting


Bookmark and Share

if that was helpful ...

check out the other tips and tricks i've compiled on these pages. you might learn something else interesting!

9 Responses to “copy a matlab array into excel”

  1. on 10 Feb 2008 at 11:41 pm Eric

    Hey check it. You can actually just display the array in the command window and select all of the array and do a command-c (copy). Then command-v (paste) into excel. Amazingly this works !!

    Also matlab can open excel files from it’s current directory browser. So you can grab a variable and open it in excel that way.

  2. on 01 Apr 2008 at 8:55 pm V

    You’re amazing. This is great for doing calculations in MATLAB then making it into a format where the administration can understand it.
    Thanks mate

  3. on 29 Oct 2008 at 2:03 pm Rick

    I get this error:

    ??? Error: File: num2clip.m Line: 24 Column: 38
    The input character is not valid in MATLAB statements or expressions.

  4. on 28 Jan 2009 at 12:10 pm Daniel

    ’ <– replace by ‘

    Then it should work.

    Thank you very much. This is a good blog.

  5. on 22 Apr 2009 at 4:21 am Anjel

    great script! thanks!

  6. on 11 Feb 2010 at 6:28 am JV

    Hi, nice script. Isn’t this the same as the “clipboard” matlab function?

    http://www.mathworks.com/access/helpdesk/help/techdoc/ref/clipboard.html

  7. on 06 Jul 2011 at 7:51 am Barney

    Legen…wait for it…DARY!

  8. on 29 Jan 2014 at 5:41 am kerem basaran

    This was just about to save me from a lot of hassle but it didn’t work.

    arraystring = reshape(arraystring,1,prod(size(arraystring))); %reshape the array to a single line

    line has a problem I guess.

    ——
    >> a=magic(2)
    a =
    1 3
    4 2
    >> num2clip(a)
    ans =
    14 32
    ——-

    When I paste the data in Excel, I get only a single row as seen in the “ans”

  9. on 04 Feb 2014 at 2:43 pm Gregg

    The function num2clip does not exist in current versions of MATLAB.

Did I get this wrong? Let me know!

Trackback URI | Comments RSS

More blogs about http://desk.stinkpot.org:8080/tricks.