passing variable to a callback function in matlab

asked 2022-04-20 07:01:46 -0500

Zac1405 gravatar image

Hey Guys, i wrote a simple code in matalb which connect my matlab with my roscore. Thee code looks as follow:


  odomSub= rossubscriber('/odom');
  odomMsg = odomSub.LatestMessage;  
  odomSubCallback = rossubscriber('/odom',@odomCallback,odomMsg, fig,1});
  pose = odomMsg.Pose.Pose;


     function [] = odomCallback(odomMsg, fig, varargin)
      % Callback function for plotting robot path on topic odom
      % fig : figure in which to plot the path
      % varargin: use for poseHandle in task 20).
      % Use the posehandle if the varargin argument is provided, otherwise use
      % the global variables
      figure(fig); % switch current figure
      hold on;
      % convert odom message to pose vector
      [ x1, y1, ~ ] = OdometryMsg2Pose( odomMsg );



function [ x, y, theta ] = OdometryMsg2Pose( odomMsg )
% convert odometry message to vector
% use quat2eul to get theta
% your code (task 15) goes here
poseMsg = odomMsg.Pose.Pose;
w = poseMsg.Orientation.W;
x = poseMsg.Position.X;
y = poseMsg.Position.Y;
z = poseMsg.Orientation.Z;
eul=quat2eul([w x y z]);


suddenly I do not get the figure 3 drawn and I really do not know why. I also think that Matlab does not go through this line.

odomSubCallback = rossubscriber('/odom',@odomCallback,odomMsg, fig,1});

because as soon as I run this code, I don't get an error message, but I also don't get the figure. and when I make a breakpoint on this line, I get recursion errors. Sometimes I have the problem that my callback function gets empty parameters.

i'll be really greatful if someone could help. I can also send you a file with the code if this could help. Thanks in advance.

edit retag flag offensive close merge delete