Hi all,

I am using scipy.io for switching from python to matlab.
here is my list of list in python
code = [[[1, 2], [3,4], [5, 6]], [[7, 8], [9, 10]]]. I want to convert this to matlab format.
For that I used a code
scipy.io.savemat('/tmp/out.mat', mdict={'Num': (code[0], code[1])}) but it is showing an error message.

tmp/<ipython console> in <module>()
/usr/lib/python2.5/site-packages/scipy/io/mio.py in savemat(file_name, mdict, appendmat)
123
124 MW = MatFile4Writer(file_stream)
--> 125 MW.put_variables(mdict)
126 if file_is_string:
127 file_stream.close()
/usr/lib/python2.5/site-packages/scipy/io/mio4.py in put_variables(self, mdict)
336 class MatFile4Writer(MatFileWriter):
337
338 def put_variables(self, mdict):
339 for name, var in mdict.items():
--> 340 matrix_writer_factory(self.file_stream, var, name).write()
/usr/lib/python2.5/site-packages/scipy/io/mio4.py in matrix_writer_factory(stream, arr, name)
322 if scipy.sparse.issparse(arr):
323 return Mat4SparseWriter(stream, arr, name)
--> 324 arr = N.array(arr)
325 dtt = arr.dtype.type
326 if dtt is N.object_:
<type 'exceptions.ValueError'>: setting an array element with a sequence.

It will be great if anyone can suggest an idea.
many thanks in advance
Vipin T S

Recommended Answers

All 6 Replies

Hi all,

I am using scipy.io for switching from python to matlab.
here is my list of list in python
code = [[[1, 2], [3,4], [5, 6]], [[7, 8], [9, 10]]]. I want to convert this to matlab format.
For that I used a code
scipy.io.savemat('/tmp/out.mat', mdict={'Num': (code[0], code[1])})

It's always helpful to use trace code:

>>> # Here's your object:
>>> code = [[[1, 2], [3,4], [5, 6]], [[7, 8], [9, 10]]]
>>> # Here's what you're passing to scipy.io.savemat:
>>> code[0]; code[1]
[[1, 2], [3, 4], [5, 6]]
[[7, 8], [9, 10]]
>>>

Is that what should be passed to savemat?

Hi,

I want to save it in a matlab file for that I gave a code like the following

>> import scipy.io
>> code = [[[1, 2], [3,4], [5, 6]], [[7, 8], [9, 10]]]
>> scipy.io.savemat('/tmp/out.mat', mdict={'Num': (code[0], code[1])})

Basically I want to store the first and second list of list as value for Num key in the matlab file.

if I try with one list of list at a time I will get the result. But when I tried to add few more values with the same key, it is showing the previously mentioned error message.

Hope you got my problem. Thank you for your time to consider my post.

It worked with

scipy.io.savemat('/tmp/out.mat', mdict={'Num0': code[0], 'Num1':code[1]})

It seems that your problem is that you're using a tuple as the value for 'Num'.

Hi ,

I am thinking of put a single key 'Num' and value as one tuple.

Hi ,

I am thinking of put a single key 'Num' and value as one tuple.

did you read the discussion here about the objects that you can pass to loadmat and savemat ?

Hi ,
Thanks for your answer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.