'How to save data for each loop?

I want to save the dictionary in Csv or mat file for each loop, the code is from the link FHN equations

   import scipy.io as sio
    trajectory_nonauto = {} 
    file_out = f"out_{i}.mat"
    for i, param in enumerate(step_sc):
        flow = partial(non_autonomous_fitzhugh_nagumo, **param)
        for j, ic in enumerate(initial_conditions):
            trajectory_nonauto[i, j] = scipy.integrate.odeint(flow,y0=ic,t=time_span)
            sio.savemat(file_out, { trajectory_nonauto[i, j]})
            data = trajectory_nonauto
            print(trajectory_nonauto)

I was getting this error

TypeError: unhashable type: 'numpy.ndarray

at sio.savemat



Solution 1:[1]

By the looks of it savemat takes a dict as its second argument.

The thing you are trying to pass, { trajectory_nonauto[i, j]}, is not a dict.

The error you are seeing is because {x} creates a set with element x, and all elements of a set must be hashable.

Solution 2:[2]

instead of sio.savemat(file_out, { trajectory_nonauto[i, j]})

try sio.savemat(file_out, {"x-axis": trajectory_nonauto[i, j]})

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 MennoK
Solution 2