I have a complex data structure
data = {}
temp = {}
data['bigdata'] = temp;
After this I copy 'key' and 'data' values from some other data structure into temp, like this
for key in backup.keys():
temp[key] = []
and
for key in backup.keys():
for val in backup[key]:
temp[key].append(val);
After that If I do
sio.savemat(outputMATfile,data,oned_as ='column')
it is giving me error saying
TTypeError: data type not understood
Is it not possible to store a complex dictionary like this in a matlab file, using python ?
MATLAB® provides a flexible, two-way integration with many programming languages, including Python. This allows different teams to work together and use MATLAB algorithms within production software and IT systems.
To convert Matlab to python, we have two options, either do it manually or take the help of some tool. To convert Matlab to python, a tool named SMOP (Small Matlab and Octave to Python Compiler) is used. This tool is capable of understanding basic Matlab code and then parsing it to python.
There are two approaches for calling MATLAB code from Python. The first is to use the MATLAB Engine API for Python, which requires a MATLAB install. The second is to use MATLAB Compiler SDK to compile a Python package that does not require users to have a MATLAB install.
edit: Answer (and question somewhat) have changed significantly to be more general. It would still be useful it the asker would tell us what kinds of objects the values in backup
are.
The scipy.io.savemat can apparently take a dictionary of dictionaries of arrays, so this structure
from numpy import array
import scipy.io
data = {
'bigdata' : {
'a' : array([1, 2, 3]),
'b' : array([1, 2, 3]),
'c' : array([1, 2, 3]),
}
}
scipy.io.savemat('test.mat', data)
loads into matlab as
>> load test.mat
>> bigdata
bigdata =
a: [3x1 int64]
c: [3x1 int64]
b: [3x1 int64]
I imagine these dictionaries can be nested up to python's recursion limit, since the implementation is recursive. I tested 6 levels of nesting dictionaries. Edit: Now you're asking about a structure like this:
data = {
'key1' : ['a' : apple, 'b' : banana],
'key2' : ['c' : crabapple, 'd' : dragonfruit],
...
}
and you haven't specified what apple, banana etc. are. It depends on what data from these Python objects you want in the Matlab objects. I tested a few classes like str (converted to char array), set (failed to convert to array), and list (array if homogeneous, character array if some strings, some numbers). The code looks pretty duck-type-ish, so if these objects have any a common data-holding interface it should get through; I present an excerpt here of the most relevant bit for the matlab5 version:
def to_writeable(source)
if isinstance(source, np.ndarray):
return source
if source is None:
return None
# Objects that have dicts
if hasattr(source, '__dict__'):
source = dict((key, value) for key, value in source.__dict__.items()
if not key.startswith('_'))
# Mappings or object dicts
if hasattr(source, 'keys'):
dtype = []
values = []
for field, value in source.items():
if (isinstance(field, basestring) and
not field[0] in '_0123456789'):
dtype.append((field,object))
values.append(value)
if dtype:
return np.array( [tuple(values)] ,dtype)
else:
return None
# Next try and convert to an array
narr = np.asanyarray(source)
if narr.dtype.type in (np.object, np.object_) and \
narr.shape == () and narr == source:
# No interesting conversion possible
return None
return narr
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With