Search code examples
pythonmatlabtranslationworkspacespyder

Import Matlab Workspace variables into Spyder (Python)


I'm translating a code from Matlab into Python, and there inevitably are some bugs. I'm going through the code comparing variables to ensure the methods are equivalent.

Is there a way to import Matlab workspace variables into Spyder (or the other way around) so I can do a boolean truth comparison for each variable?


Solution

  • I saved the Matlab workspace as .mat file at 'File Location'

    import h5py
    import numpy as np
    f = h5py.File('File Location')
    matlab_arr=f['array name']
    matlab_arr=np.array(matlab_arr,dtype='f8')
    

    Could then do a comparison with:

    (matlab_arr==python_arr).all()
    

    or

    np.isclose(matlab_arr, python_arr).all()