Save File As. Mat in Python

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.

1 Answer

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})
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sophia Al-Mansoor
Author

Sophia Al-Mansoor

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.