View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Display Python Formatted Arrays

5.0 | 1 rating Rate this file 11 Downloads (last 30 days) File Size: 1.55 KB File ID: #24087 Version: 1.3

Display Python Formatted Arrays


Robin (view profile)


12 May 2009 (Updated )

Displays a double array in Python/NumPy format

| Watch this File

File Information

This class inherits from double and overloads the display function to output multidimensional arrays in text format that is compatible for copy and pasting into an interactive Python session.

It doesn't quite work with arbitrary dimensions, since I got stuck with generalised indexing, hence the hackish:
newx = squeeze(x(i,:,:,:,:,:,:,:,:,:,:));
(I'd be interested to hear if theres a different way to do this!)
So at the moment it supports about 12 dimensions, which I thought was probably enough for most copy + paste needs, but if you need more you can just add more colons to the line above - it doesn't seem to do any harm!

Anyway with the file in your Matlab path:
>> x = rand(2,3,4,5);
>> a = array(x)

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

>> 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]]])

MATLAB release MATLAB 7.7 (R2008b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (2)
09 Aug 2013 Shahab

Shahab (view profile)

13 May 2009 Robin

Robin (view profile)

If anyone is interested Python code for other direction (to format Python arrays for copy and pasting into an interactive MATLAB session is available here:


Comment only
12 May 2009 1.1

More detailed description with an example.

03 Dec 2009 1.2

Update file - fix problem with column array

13 Mar 2010 1.3

another silly bug - missing off first element...

Contact us