1

I have a struct in MATLAB which looks like this:

model(1:2).energy(1:3).voltage(1:4).time(1:10000)  
model(1:2).energy(1:3).voltage(1:4).current(1:10000)

The basic operation I am doing is plotting the current vs time.

I'd like to start using python, but I am not very familiar with it. What kind of python structure could I use which has similar functionality?

From what I have seen a nested class could do the job. Is there another more efficient way?

2
  • 1
    This looks like a pretty poor structuring of your data in MATLAB. Mostly because it makes it so hard to use, as opposed to a simple matrix time(model,energy,voltage) and current(model,energy,voltage). Also much more memory intensive. Commented Aug 16, 2019 at 15:07
  • @CrisLuengo Thanks for the suggestion. This approach seems indeed much simpler. The main reason I was using the struct format in MATLAB was because of the possibility to save it as a .mat file on my computer. Commented Aug 16, 2019 at 15:22

1 Answer 1

2

There are quite a few options you can use dictionaries or as you said construct your own classes. If you are migrating from matlab also consider the numpy module since it makes operating arrays (or matrix) a breeze. Below is a short example of how to use them.

import numpy as np
import matplotlib.pyplot as plt

time = np.linspace(0.0, 10.0, 51)
current = np.random.randint(115,125,size=(51))
voltage =  current*(time**2)


#Dictionary Example
model_dict = {'time': time, 'voltage': voltage, 'current': current}
plt_attr = {'color': ['r','b','g','k']}

fig, ax1 = plt.subplots(2,1)
ax1[0].title.set_text('Dictionary Example')
ax1[0].set_xlabel('time (s)')
ax1[0].set_ylabel('Voltage [V]', color=plt_attr['color'][0])
# Note how the variables are called
ax1[0].plot(model_dict['time'], model_dict['voltage'], color=plt_attr['color'][0])
ax1[0].tick_params(axis='y', labelcolor=plt_attr['color'][0])
ax2 = ax1[0].twinx()  # instantiate a second axes that shares the same x-axis

ax2.set_ylabel('Current [Amp]', color=plt_attr['color'][1])  # we already handled the x-label with ax1
# Note how the variables are called
ax2.plot(model_dict['time'], model_dict['current'], color=plt_attr['color'][1])
ax2.tick_params(axis='y', labelcolor=plt_attr['color'][1])

#Class Example
class model:
    def __init__(self, name, i = None, v = None, e = None, t = None):
        self.name = name
        self.current = i
        self.voltage = v
        self.energy = e
        self.time = t

model_class = model('model_1', i = current, v = voltage, t = time)   

ax1[1].title.set_text('Class Example')
ax1[1].set_xlabel('time (s)')
ax1[1].set_ylabel('Voltage [V]', color=plt_attr['color'][2])
# Note how the variables are called
ax1[1].plot(model_class.time, model_class.voltage, color=plt_attr['color'][2])
ax1[1].tick_params(axis='y', labelcolor=plt_attr['color'][2])

ax2 = ax1[1].twinx()  # instantiate a second axes that shares the same x-axis
ax2.set_ylabel('Current [Amp]', color=plt_attr['color'][3])  # we already handled the x-label with ax1
# Note how the variables are called
ax2.plot(model_class.time, model_class.current, color=plt_attr['color'][3])
ax2.tick_params(axis='y', labelcolor=plt_attr['color'][3])
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.