image thumbnail

Display Python Formatted Arrays

version 1.3.0.0 (1.55 KB) by Robin
Displays a double array in Python/NumPy format

1K Downloads

Updated Sat, 13 Mar 2010 14:26:36 +0000

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

Cite As

Robin (2022). Display Python Formatted Arrays (https://www.mathworks.com/matlabcentral/fileexchange/24087-display-python-formatted-arrays), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2008b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!