3

I am reading in information via serial being sent by an Arduino. This is some example data being read:

4
1
5
2
15
1

Being a previous MATLAB user, I would just be able to copy the values and so something like

a = PASTE

to get a vector of the values. Is there a similar way to getting all the values in an int array or a matrix in the python terminal (using numpy and matplotlib for further plotting)?

Thanks for the help.

5
  • Please elaborate on a = PASTE? Commented Jan 8, 2014 at 10:43
  • Sorry. I meant I could just copy the data (as shown) into MATLAB such as "a = PASTECOMMAND". That, of course in MATLAB, would make a vector a consisting of the data. Commented Jan 8, 2014 at 10:46
  • I would think you would have to type a = [ then paste then type ] and then press enter? So I'm sure you can do that in Python too? What Python IDE are you using? Commented Jan 8, 2014 at 10:47
  • I tried a = [ PASTE ] before posting this question, didn't work. I'm just using the Mac terminal. Commented Jan 8, 2014 at 11:00
  • I don't know about Mac, but if you're used to Matlab you should take a look at the Spyder IDE Commented Jan 8, 2014 at 11:11

2 Answers 2

3

If you use IPython, you can :

>>> import numpy as np
>>> %paste x
>>> x = np.array(x, int)
Sign up to request clarification or add additional context in comments.

2 Comments

Tried it, but getting this error: ValueError: invalid literal for long() with base 10: 'import numpy as np'
You need copy the data to the clipboard and input '%paste x' in ipython console.
1

Basically, this works in Matlab because spaces are used to separate items in a sequence, and Matlab will let you use newlines instead of spaces. In other words, you can do x = [1 2 3]

In python, commas are used instead. In other words, you need to do x = [1, 2, 3].

Python will let you have newlines in an expression if you've opened a brace/bracket, etc. For example, you can do:

In [1]: x = [1,
   ...: 2,
   ...: 3,
   ...: 4]

In [2]: x
Out[2]: [1, 2, 3, 4]

...but you still need the commas for it to be valid syntax.

If you want to use newlines as a seperator for the sequence (that you've presumably copied to the clipboard), you'll need to explicitly split the string on newlines.

To start a multi-line string, use triple-quotes. (""" or ''')

For example: (I've typed x = """ and then hit paste (e.g. <ctrl>-v/<shift>-<ins>/whatever))

In [1]: x = """4
   ...: 1
   ...: 5
   ...: 2
   ...: 15
   ...: 1"""

In [2]: x
Out[2]: '4\n1\n5\n2\n15\n1'

In [3]: x.split()
Out[3]: ['4', '1', '5', '2', '15', '1']

In [4]: import numpy as np

In [5]: np.array(x.split(), dtype=float)
Out[5]: array([  4.,   1.,   5.,   2.,  15.,   1.])

Also, as @HYRY mentioned, if you're using ipython, it will do the equivalent of

In [1]: x = """4
   ...: 1
   ...: 5
   ...: 2
   ...: 15
   ...: 1"""

In [2]: x = x.split()

With just:

In [1]: %paste x

Better yet, if you're reading data in from the serial port, just read it directly into python. Have a look at pyserial: http://pyserial.sourceforge.net/

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.