Proper importing of MATLAB structures into Python
217 次查看(过去 30 天)
显示 更早的评论
I have a MATLAB (2017b) structure which is saved (attached) as lib.mat. This structure, at its highest level, is basically a library with three different fields including an array of structures: ['name', 'structures' [1 x 32 struct], 'structlist', {1 x 32 cell}].
I have tried two different methods to import this structure into Python (v3.5):
a) Import using the io module from Scipy library
import scipy.io as spio
lib = spio.loadmat('lib.mat')
which produces a dict in python called 'lib' whose fields are not accessible (at least I am not aware how to access, for example, the 32 structures listed in the field 'structures' [1 x 32 struct]).
b) calling MATLAB engine in Python (v3.5) and using getfield function to access different fields of the structure:
import matlab.engine
eng = matlab.engine.start_matlab()
Obj = eng.load('lib.mat')['lib']
Names = eng.getfield(Obj, 'name')
Strs = eng.getfield(Obj, 'structures')
StrLists = eng.getfield(Obj, 'structlist')
where everything works but
Strs = eng.getfield(Obj, 'structures')
is not able to restore the 32 structures in variable Strs. For example if you try:
>>> eng.size(StrLists)
the result would be: matlab.double([[1.0,32.0]]), which is correct, but if you try
>>> eng.size(Strs)
the results is: matlab.double([[0.0,0.0]]), which is incorrect (must be the same size as StrList).
Now my question is how to properly import such data structures into Python and access to their fields.
Thanks
0 个评论
回答(1 个)
Bo Li
2018-2-20
Would you share more details about how the file "lib.mat" is created? I tried following steps:
>>lib.name='hello'
>>Strs(1).id=1
>>Strs(2).id=2
>>Strs(3).id=3
>>lib.Strs = Strs
>>lib.StrList = {1,2,3}
>>save('lib', 'lib')
This what I see in Python:
>>>Obj = eng.load('lib.mat')['lib']
...
ValueError: only a scalar struct can be returned from MATLAB
This is the expected behavior as Struct array is not supported according to the doc:
https://www.mathworks.com/help/matlab/matlab_external/handle-data-returned-from-matlab-to-python.html
In this case, "lib" itself is a scalar struct but the element "Strs" is a struct array.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call Python from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!