Rosanswers logo


I'm using Matlab/ROS interface (with Matlab R2013a and ROS fuerte)

I've done the Matlab example where a node publish and subscriber from Matalb and this example works without problem. But now I have gazebo running and I want to subscrib from Matlab and It doesn't work.. Could anyone tell me what is wrong?

I do: roscore

roslaunch gazebo_world ....

and execute this code from Matlab:

global rosMasterIp; rosMasterIp = '';

global localhostIp; localhostIp = '';

% Create a new node named /NODE and connect it to the master.

node = rosmatlab.node('NODE', rosMasterIp, 11311, 'rosIP', localhostIp);

% Add a publisher of a topic named /TOPIC to the node to send message of type std_msgs/String.

publisher = rosmatlab.publisher('TOPIC','std_msgs/String',node);

% Add a subscriber to a topic named /TOPIC to the node to receive message

subscriber = rosmatlab.subscriber('TOPIC','std_msgs/String',1,node)

%Gazebo topic

subscriber2 = rosmatlab.subscriber('gazebo/model_states', 'gazebo_msgs/ModelStates',1,node);

subscriber.setOnNewMessageListeners({@function1}); subscriber2.setOnNewMessageListeners({@function2});

% Create a new message of type std_msgs/String.

msg = rosmatlab.message('std_msgs/String',node);

% Set the data field of the message and then publish the message

msg.setData(sprintf('Message created: %s',datestr(now)));

publisher.publish(msg); pause(1);

Originally posted by Sanxano on ROS Answers with karma: 70 on 2014-03-27

Post score: 0


1 Answer 1


Rosanswers logo

I added two lines to ~/.bashrc to make sure roscore has a predictable URI:

export ROS_IP= <== your computer's IP


To connect in MATLAB:

% Run the rosmatlab setup scripts

addpath([matlabroot '/toolbox/psp/rosmatlab/']);



% Connect to roscore which should be running in a terminal

node = rosmatlab.node('MATLAB_NODE', '', 11311);

% Subscribe to the 'state' topic to receive updates on the state.

% The message type is lyap_control/plant

subscriber = rosmatlab.subscriber('state', 'lyap_control/plant', 1, node);

Originally posted by AndyZe with karma: 2331 on 2015-01-04

This answer was ACCEPTED on the original site

Post score: 1

Original comments

Comment by dottant on 2015-03-02:
Does it work or not?? I can't subscribe from Matlab to ros topic, it seems to be unidirectional. Please, let me know if you solved it and share the full solution! thanks!!!

Comment by ziyeshanwai on 2015-03-28:
I want to know ,too.I meet the same problem give you a link,maybe can help you link text

Comment by AndyZe on 2015-03-28:
Yeah, it works, guys. Have you tried it? Make sure the ip that you specify with ROS_IP is your computer's current IP.

Comment by ziyeshanwai on 2015-03-29:
Yes,I have tried.I worked!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.