'Save file as .mat in python

I want to save a list of elements (r) to .mat file format inside python I convert it to array

r = Lattice([Marker('l000015$start', NumIntSteps=40, length=array(0.)), Marker('ip.1', NumIntSteps=40, length=array(0.)), Drift('drift_0', 2.2002250210956804), Quadrupole('qc1l1.1', 1.2, -0.6451875623775719, NumIntSteps=40), Monitor('BPM0'), Drift('drift_1', 0.08000000000000007), Monitor('BPM1'), Quadrupole('qc1r2.1', 1.0, 0.34045204285588043, NumIntSteps=40), Monitor('BPM2'), Drift('drift_2', 0.08000000000000007), Monitor('BPM3'), Quadrupole('qc1r3.1', 1.0, 0.18818867758026042, NumIntSteps=40), Monitor('BPM4'), Drift('drift_3', 0.2999999999999998), Quadrupole('qc2r1.1', 1.25, 0.026716664923441297, NumIntSteps=40), Monitor('BPM5'), Drift('drift_4', 0.08000000000000096), Monitor('BPM6'), Quadrupole('qc2r2.1', 1.25, -0.02041914442905664, NumIntSteps=40), Monitor('BPM7'),..........



import numpy as np
import scipy.io
arr = np.array(r)

Then i tried to save it using

scipy.io.savemat('file.mat', arr)

I got the error massage

AttributeError: 'numpy.ndarray' object has no attribute 'items'

Could you please clarify to me what is the "items" means,

I imported this list from .mat in python then i added some elements to it inside python and now i want to save it in .mat format.



Solution 1:[1]

The second argument needs to be a dictionary containing all variables that you want to save. So, we can fix your code as follows.

scipy.io.savemat('file.mat', {'matrix_name':arr})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ben Grossmann