File Exchange

image thumbnail

Display Python Formatted Arrays

version 1.3 (1.55 KB) by

Displays a double array in Python/NumPy format

4 Downloads

Updated

View License

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'

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

Comments and Ratings (2)

Shahab

Shahab (view profile)

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:
http://codepad.org/MVYCM0AJ

Robin

Updates

1.3

another silly bug - missing off first element...

1.2

Update file - fix problem with column array

1.1

More detailed description with an example.

MATLAB Release
MATLAB 7.7 (R2008b)

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

» Watch video