Save Python List to MAT File (R2015b)

조회 수: 17 (최근 30일)
Peter Cook
Peter Cook 2017년 9월 28일
답변: Divyendu Narayan 2018년 2월 27일
I've got a small apparent issue with using the python list datatype in MATLAB. If I save a py.list to a MAT file and then try to reload it, I get an empty list in the workspace.
In this example, I've got a list of lists I am trying to hash so I don't have to scan a drive every time I run the program.
>> jobFileList.length()
ans =
3
>> save('C:\Program Files\HeavyOilToolbox\jobFileList.mat','-v7.3','jobFileList')
>> load('C:\Program Files\HeavyOilToolbox\jobFileList.mat','jobFileList')
>> jobFileList.length()
ans =
0
Here's me trying to save a cell array of lists instead
>> jobFileListCell = cell(jobFileList)
jobFileListCell =
[1x65073 py.list] [1x574 py.list] [1x439 py.list]
>> save('C:\Program Files\HeavyOilToolbox\jobFileListCell.mat','-v7.3','jobFileListCell')
>> load('C:\Program Files\HeavyOilToolbox\jobFileListCell.mat','jobFileListCell')
>> jobFileListCell
jobFileListCell =
[1x0 py.list] [1x0 py.list] [1x0 py.list]
My current workaround is to use
py.numpy.save('C:\\Program Files\\HeavyOilToolbox\\jobFileList.npy',jobFileList)
jobFileList = py.numpy.load('C:\\Program Files\\HeavyOilToolbox\\jobFileList.npy')
jobFileList = py.list(jobFileList)
But I am hoping there is a solution in native MATLAB. Any insight?
  댓글 수: 1
Peter Cook
Peter Cook 2017년 9월 28일
I forgot to mention, this behavior seems independent of the -v7.3 flag passed to save().

댓글을 달려면 로그인하십시오.

답변 (2개)

Robert Snoeberger
Robert Snoeberger 2017년 9월 29일
"Saving (serializing) Python objects into a MAT-file" is listed as a limitation in the documentation [1].
  1. https://www.mathworks.com/help/matlab/matlab_external/limitations-to-python-support.html

Divyendu Narayan
Divyendu Narayan 2018년 2월 27일
You can overcome this limitation by using scipy.io https://docs.scipy.org/doc/scipy-0.19.0/reference/generated/scipy.io.savemat.html
Below link has answered this: https://stackoverflow.com/questions/9232751/saving-and-loading-python-dict-with-savemat-results-in-error

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by