Apress / python-for-matlab-development

Source Code for 'Python for MATLAB Development' by Albert Danial
Other
39 stars 20 forks source link

mat2py: Multidimensional array support #8

Closed harry-commin-enclustra closed 5 months ago

harry-commin-enclustra commented 5 months ago

Passing a multidimensional array (> 2D) to mat2py() currently fails silently.

For example, this returns false, with no error message:

a = randn(3,4,5);
b = py2mat(mat2py(a));
isequal(a, b)

I think this can be fixed by simply replacing the following line with else:

https://github.com/Apress/python-for-matlab-development/blob/6bf5d448131fb08f2470f3c435853636aae223b2/code/matlab_py/mat2py.m#L84

This causes the example above to return true. (And it also prevents silent failures, so no need to print an error message for unhandled cases). I would expect this to work find for all the data types, but I haven't tested them all:

        case {'double', 'single', ...
              'uint8', 'uint16', 'uint32', 'uint64', ...
              'int8',  'int16',  'int32',  'int64'}
AlDanial commented 5 months ago

Thanks for the bug report and PR! It will be a week or so before I can get to this but I have every intention of merging.

AlDanial commented 5 months ago

Sure enough it was that easy; pushed bbe0424