ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

string to a float

asked 2016-07-25 07:49:58 -0600

chezzydc10 gravatar image

updated 2016-07-29 07:24:27 -0600

Hi, I will like to change the string message of one of my topics to a float

edit retag flag offensive close merge delete


Does your topic output's numbers in string format?

krishna43 gravatar image krishna43  ( 2016-07-25 10:50:19 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2016-07-27 15:56:13 -0600

Chrissi gravatar image

updated 2016-07-28 09:58:22 -0600

Not nice but the easiest is to just republish it I guess.

import rospy
from std_msgs.msg import String, Float64

p = rospy.Publisher("/topic_out", Float64)

def callback(msg):

rospy.Subscriber("/topic_in", String, callback)

EDIT: You can also write a script that reads from the bag and writes to a new one. Have a look at the rosbag API for that.


I am not entirely sure how you subscribe to what but here is how it works for me:

Let's assume I have a node like this that produces the data:

import rospy
import numpy as np
from std_msgs.msg import String


p = rospy.Publisher("/string_topic", String, queue_size=1)

Fs = 8000
f = 5
sample = 8000
x = np.arange(sample)
y = np.sin(2 * np.pi * f * x / Fs)

r = rospy.Rate(10)
for e in y:

which just produces a sine wave and then I save that to a rosbag rosbag record /string_topic which generates the following bag

path:        2016-07-28-08-06-26.bag
version:     2.0
duration:    2:46s (166s)
start:       Jul 28 2016 08:06:29.43 (1469689589.43)
end:         Jul 28 2016 08:09:16.33 (1469689756.33)
size:        129.3 KB
messages:    1670
compression: none [1/1 chunks]
types:       std_msgs/String [992ce8a1687cec8c8bd883ec73ca41d1]
topics:      /string_topic   1670 msgs    : std_msgs/String

This I can then play rosbag play 2016-07-28-08-06-26.bag and use the script I posted above to subscribe to /string_topic:

import rospy
from std_msgs.msg import String, Float64

p = rospy.Publisher("/float_topic", Float64)

def callback(msg):

rospy.Subscriber("/string_topic", String, callback)

I can the connect /float_topic to rqt_plot and see my data. My connection graph looks likes this:


Where on the left you have the rosbag and on the right the rqt_plot.

Maybe your bag contains just the same number in all the messages? Can you upload it somewhere or post some of the example output of the topic you want to plot. Also, the output of rosbag info on your bag would be good.


To accommodate the data you got, we would need a different conversion node:

import rospy
from std_msgs.msg import String, Float64

publishers = []
for i in range(6):
    publishers.append(rospy.Publisher("/thermal_zone"+str(i), Float64))

def callback(msg):
    for s, p in zip('\n'), publishers):

rospy.Subscriber("/string_topic", String, callback)

This is not tested as I do not have the data but it creates 6 publishers which are called thermal_zone0 to thermal_zone5 like in your data. In the callback it tries to split the incoming string based on the line break \n which I assume is in there but might be wrong. It then splits the string again based on the : and takes the right part of it which should be the number and then publishes it as a ... (more)

edit flag offensive delete link more


When i republished it as float, i was not getting the same data i had on my rosbag. I was getting the same number over and over again

chezzydc10 gravatar image chezzydc10  ( 2016-07-27 16:26:38 -0600 )edit

Python or cpp? Can't imagine how that could happen given above python script. In cpp it might because of a wrong string conversion.

Chrissi gravatar image Chrissi  ( 2016-07-27 16:46:02 -0600 )edit

In python. From the sample code u wrote, you are subscribing to the node but im subscribing to the rosbag in mine. I already have some recorded data in rosbag which plays as string but i will like them to play as float

chezzydc10 gravatar image chezzydc10  ( 2016-07-27 17:04:50 -0600 )edit

See edit in answer

Chrissi gravatar image Chrissi  ( 2016-07-28 03:16:08 -0600 )edit

I followed your first code but after playing the rosbag and running the python script, I got this error in the link below. The new node couldn't convert the string to float.

chezzydc10 gravatar image chezzydc10  ( 2016-07-28 07:02:25 -0600 )edit

Your topic publishes a weird string like: "/sys/device/.../../:35000" and not just a number like "35000.0" so python of course doesn't know how to convert this to a float. You have to parse the string so you only have the number before doing float(). If you want help please edit your question

Chrissi gravatar image Chrissi  ( 2016-07-28 07:56:10 -0600 )edit

... and post some example output of your topic. Also please post the output of rosbag info of your bag just to make sure it is really just publishing a string message. Looks like it is publishing a number of strings in the same message.

Chrissi gravatar image Chrissi  ( 2016-07-28 07:56:33 -0600 )edit

This is the example of my topic when you echo it. The data has already been recorded so this is what i get when I play the rosbag. Also this is what i have in my rosbag info

chezzydc10 gravatar image chezzydc10  ( 2016-07-28 08:38:22 -0600 )edit

Question Tools

1 follower


Asked: 2016-07-25 07:49:02 -0600

Seen: 1,983 times

Last updated: Jul 29 '16