I have multiple sliders inside a figure, and I would like to publish their values (using the robotics toolbox) when one of them changes. I'm unable to access the value of more than one slider - an attempt to access the slider values gives me the following error in the sliderMoving
function:
Undefined variable "event1" or class "event1.Value".
The code is as follows:
function sliderchanging
%rosinit('129.78.214.177');
first_val = 0;
euler_v = rossubscriber('/Euler_values');
slider_v = rospublisher('/Slider_values', 'std_msgs/Float64MultiArray');
slidermsg = rosmessage(slider_v);
eulermsg = rostopic('echo', '/Euler_values');
ypr = eulermsg.Data;
y = ypr(1);
p = ypr(2);
r = ypr(3);
fig = uifigure;
sld = uislider(fig,'Position',[20 50 260 20],'ValueChangingFcn',@(sld,event) sliderMoving(event, slidermsg, slider_v, y, p, r));
sld1 = uislider(fig,'Position',[20 80 260 20],'ValueChangingFcn',@(sld1,event1) sliderMoving(event1, slidermsg, slider_v, y, p, r));
if first_val == 0
send(slider_v, eulermsg);
end
sld.Limits = [y-2 y+2];
sld.Value = y;
sld.Position = [20 50 260 20];
sld1.Limits = [p-2 p+2];
sld1.Value = p;
sld1.Position = [20 80 260 20];
end
function sliderMoving(event, slidermsg, slider_v, y, p, r)
first_val = 1;
disp(event.Value)
disp(event1.Value)
slidermsg.Data = [event.Value, p, r];
send(slider_v, slidermsg)
end
What is wrong with this code? How can I access the values of all available sliders from within the sliderMoving
callback?
Your problem is because the sliderMoving
function does not know the name of the variable in the calling workspace. In other words,
function out = func(in)
% do something with in
end
won't behave differently if we call it like func(in1)
or func(in2)
.
In your case, the event will always be known inside the callback as event
.
If you want different behavior based on which slider was used, you should decide based on the first two inputs to the callback (commonly: src
and eventData
), or via some additional input parameter (as you already do). If you need to access the value of the other slider, you can do this using event.Source.Parent.otherSld
.
You should also note that the statement first_val = 1;
that is found inside the callback has no effect on the value outside the callback. You should read about nested functions.
I think this is what you meant to do:
function sliderchanging
...
fig = uifigure;
...
function sliderMoving(...)
end
end