0

I guess it is a simple question, I am doing simple while iteration and want to save data within data array so I can simple plot it.

tr = 25 #sec
fr = 50 #Hz
dt = 0.002 #2ms
df = fr*(dt/tr)
i=0;
f = 0
data = 0

while(f<50):
    i=i+1
    f = ramp(fr,f,df)
    data[i] = f

plot(data)  

How to correctly define data array? How to save results in array?

0

4 Answers 4

3

One possibility:

data = []

while(f<50):
    f = ramp(fr,f,df)
    data.append(f)

Here, i is no longer needed.

Sign up to request clarification or add additional context in comments.

Comments

3

you could initialize a list like this:

data=[]

then you could add data like this:

data.append(f)

5 Comments

I am getting: SyntaxError: can't assign to function call
@Pepe: you can't get that error from these commands. You would only get that message if you tried something like data.append(i) = f. You probably want to use something like aix's data.append(f).
@NedBatchelder obviously the OP is not familiarized with python, so that's why I used the word "array". Flagged as not constructive
@Pepe sorry as @DSM says you should use data.append(f)
@pacofvf: Since the word has been changed, I take back my -1!
0

For plotting matplotlib is a good choice and easy to install and use.

import pylab

pylab.plot(data)
pylab.show()

Comments

-1

He needs "i" b/c it starts from 1 in the collection. For your code to work use:

data = {} # this is dictionary and not list

1 Comment

The OP is asking about how to setup an array, there is no dict involved here. data[i] = f would be an acceptable way to index a data array if it had been initialized with something like data=[0.0]*1000.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.