## copy a matlab array into excel

January 29th, 2006 by Lawrence David

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 linearraystringshift = [' ',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

on 10 Feb 2008 at 11:41 pmEricHey 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.

on 01 Apr 2008 at 8:55 pmVYou’re amazing. This is great for doing calculations in MATLAB then making it into a format where the administration can understand it.

Thanks mate

on 29 Oct 2008 at 2:03 pmRickI get this error:

??? Error: File: num2clip.m Line: 24 Column: 38

The input character is not valid in MATLAB statements or expressions.

on 28 Jan 2009 at 12:10 pmDaniel’ <– replace by ‘

Then it should work.

Thank you very much. This is a good blog.

on 22 Apr 2009 at 4:21 amAnjelgreat script! thanks!

on 11 Feb 2010 at 6:28 amJVHi, nice script. Isn’t this the same as the “clipboard” matlab function?

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

on 06 Jul 2011 at 7:51 amBarneyLegen…wait for it…DARY!

on 29 Jan 2014 at 5:41 amkerem basaranThis 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”

on 04 Feb 2014 at 2:43 pmGreggThe function num2clip does not exist in current versions of MATLAB.