Convert numpy ndarray to matlab mlarray in python

20 vues (au cours des 30 derniers jours)
Gan Lee
Gan Lee le 14 Avr 2022
Commenté : Christopher Wunder le 23 Août 2022
mlarray to ndarray: np.asarray(x._data, dtype=dtype)
but inversely, ndarray to mlarray: matlab.double(x.tolist()), which is extremely slow, is there a more efficient way to do this? Thank u for answering.

Réponse acceptée

Al Danial
Al Danial le 28 Avr 2022
Looks like you're making the data load unnecessarily complicated. The file actually loads cleanly into a NumPy array:
from scipy.io import loadmat
data = loadmat('data.mat', squeeze_me=True)
x = data['data']
print(x)
produces
array([[0.00000000e+00, 5.90000000e+01, 5.90000000e+01, 2.25296241e+05],
[1.00000000e+00, 6.20000000e+01, 5.81599120e+01, 5.93159561e+04],
[2.00000000e+00, 1.00000000e+02, 9.47518190e+01, 3.22666379e+04],
...,
[2.04500000e+03, 4.00000000e+00, 4.88991300e+00, 3.01840538e+04],
[2.04600000e+03, 2.00000000e+00, 2.26899200e+00, 6.46032757e+04],
[2.04700000e+03, 1.00000000e+00, 1.00000000e+00, 1.18671912e+05]])
Simplify your function load_mat to
def load_mat(pth_mat, key=None):
data = loadmat(pth_mat, squeeze_me=True)
print(data.keys()) if key is None else None
return data[key]
then call it like this
x1 = load_mat('data.mat', 'data')

Plus de réponses (3)

Al Danial
Al Danial le 21 Avr 2022
Which version of MATLAB? 2020a and newer (I don't have easy access to older versions) should just be able to do
>> mx = double(x);
without a conversion to a list.
  2 commentaires
Gan Lee
Gan Lee le 24 Avr 2022
Thanks. Its 2021b , I tried this but not available.
Christopher Wunder
Christopher Wunder le 23 Août 2022
This is not possible for me either.
The function _is_initializer in matlab._internal.mlarray_utils.py checks for the input to be of type collections.abc.Sequence and a numpy.ndarray fails to be of such a type. The only way (without altering the package) is to convert the array beforehand or pass any kind of Sequence to it instead of an array.

Connectez-vous pour commenter.


Al Danial
Al Danial le 24 Avr 2022
Now I'm curious what is in your variable x. Can you make a small version of this data, write it to a .mat file, then attach the .mat file?

Gan Lee
Gan Lee le 27 Avr 2022
Modifié(e) : Gan Lee le 27 Avr 2022
Here is part of my python code:
# -*- coding: utf-8 -*-
import numpy as np
from scipy.io import loadmat
import h5py
import matlab
from matlab import engine
from matlab import mlarray
def load_mat(pth_mat, key=None):
data = loadmat(pth_mat)
print(data.keys()) if key is None else None
return data.get(key)[:].astype(np.double)
def mlarray2ndarray(x: mlarray):
return np.asarray(x._data, dtype=np.double)
def ndarray2mlarray(x: np.ndarray):
return matlab.double(x)
if __name__ == "__main__":
eng = engine.start_matlab()
pth_mat = r".\data.mat"
# np to matlab
x1: np.ndarray = load_mat(pth_mat, key="data")
# mx1: mlarray = ndarray2mlarray(x1) #WRONG
mx1 = matlab.double(x.tolist()) #OK, BUT VERY SLOWLY
# matlab to np
mx2: mlarray = eng.load(pth_mat).get("data")
x2: np.ndarray = mlarray2ndarray(mx2) #OK
# ......
eng.exit()
here is errcode:
ValueError
initializer must be a rectangular nested sequence

Catégories

En savoir plus sur Platform and License dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by