Send As SMS

1/29/2006

copy a matlab array into excel

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


0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home

My blog has moved! Redirecting...

You should be automatically redirected. If not, visit http://stinkpot.afraid.org:8080/tricks/ and update your bookmarks.