[Numpy-discussion] copy and paste arrays from matlab

Robin robince@gmail....
Tue May 12 08:12:12 CDT 2009


[crossposted to numpy-discussion and mlabwrap-user]

Hi,

I wrote a little utility class in Matlab that inherits from double and
overloads the display function so you can easily print matlab arrays
of arbitrary dimension in Numpy format for easy copy and pasting.

I have to work a lot with other peoples code - and while mlabwrap and
reading and writing is great, sometimes I find it easier and quicker
just to copy and paste smaller arrays between interactive sessions.

Anyway you put it in your Matlab path then you can do
x = rand(2,3,4,5);
a = array(x)

You can specify the fprintf style format string either in the
constructor or after:
a = array(x,'%2.6f')
a.format = '%2.2f'

eg:
>> x = rand(4,3,2);
>> array(x)
ans =

array([[[2.071566461449581e-01, 3.501602151029837e-02],
        [1.589135260727248e-01, 3.766891927380323e-01],
        [8.757206127846399e-01, 7.259276565938600e-01]],

       [[7.570839415557700e-01, 3.974969411279816e-02],
        [8.109207856487061e-01, 5.043242527988604e-01],
        [6.351863794630047e-01, 7.013280585980169e-01]],

       [[8.863281096304466e-01, 9.885678912262633e-01],
        [4.765077527169480e-01, 7.634956792870943e-01],
        [9.728134909163066e-02, 4.588908258125032e-01]],

       [[4.722298594969571e-01, 6.861815984603373e-01],
        [1.162875322461844e-01, 4.887479677951201e-02],
        [9.084394562396312e-01, 5.822948089552498e-01]]])

It's a while since I've tried to do anything like this in Matlab and I
must admit I found it pretty painful, so I hope it can be useful to
someone else!

I will try and do one for Python for copying and pasting to Matlab,
but I'm expecting that to be a lot easier!

Cheers

Robin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: array.m
Type: application/octet-stream
Size: 2103 bytes
Desc: not available
Url : http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090512/8a30e71b/attachment.obj 


More information about the Numpy-discussion mailing list