[Numpy-discussion] copy and paste arrays from matlab

Robin robince at gmail.com
Wed May 13 12:39:01 EDT 2009


[crossposted to numpy-discussion and mlabwrap-user]

Hi,

Please find attached Python code for the opposite direction - ie
format Python arrays for copy and pasting into an interactive Matlab
session.

It doesn't look as nice because newlines are row seperators in matlab
so I put everything on one line. Also theres no way to input >2D
arrays in Matlab that I know of without using reshape.

In [286]: from mmat import mmat
In [289]: x = rand(4,2)
In [290]: mmat(x,'%2.3f')
[ 0.897 0.074 ;   0.005 0.174 ;   0.207 0.736 ;   0.453 0.111 ]
In [287]: mmat(x,'%2.3f')
reshape([  [ 0.405 0.361 0.609 ;   0.249 0.275 0.620 ;   0.740 0.754
0.699 ;   0.280 0.053 0.181 ] [ 0.796 0.114 0.720 ;   0.296 0.692
0.352 ;   0.218 0.894 0.818 ;   0.709 0.946 0.860 ] ],[ 4 3 2 ])
In [288]: mmat(x)
reshape([  [ 4.046905655728e-01 3.605995195844e-01 6.089653771166e-01
;   2.491999503702e-01 2.751880043180e-01 6.199629932480e-01 ;
7.401974485581e-01 7.537929345351e-01 6.991798908866e-01 ;
2.800494872019e-01 5.258468515210e-02 1.812706305994e-01 ] [
7.957907133899e-01 1.144010574386e-01 7.203522053853e-01 ;
2.962977637560e-01 6.920657079182e-01 3.522371076632e-01 ;
2.181950954650e-01 8.936401263709e-01 8.177351741233e-01 ;
7.092517323839e-01 9.458774967489e-01 8.595104463863e-01 ] ],[ 4 3 2
])

Hope someone else finds it useful.

Cheers

Robin

On Tue, May 12, 2009 at 2:12 PM, Robin <robince at gmail.com> wrote:
> [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: mmat.py
Type: application/octet-stream
Size: 1363 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20090513/c967821f/attachment.obj>


More information about the NumPy-Discussion mailing list