MATLAB 2015 slider gui question

2 次查看(过去 30 天)
John
John 2015-3-18
编辑: Stephen23 2015-3-29
I have MATLAB 2014a and I know that the GUI slider does not have well-documented functionality for continuous slider callbacks (i.e. the slider callback is executed as you drag the slider and not just when you drop the slider at a new position with a mouse key release).
Since then MATLAB has jumped to 2 new versions, with 2015 being the latest, and since then has undergone significant changes in the UI implementation.
So I was wondering if in the new MATLAB GUI framework, the slider functionality has been enhanced to allow the callback to be called whenever the slider is dragged. I could not find any information in the official MATLAB documentation.

回答(3 个)

YoGabbaGabba
YoGabbaGabba 2015-3-27
This is actually technically possible but annoyingly difficult. First, a figure has a WindowButtonDownFcn and WindowButtonUpFcn that will tell you when the user has clicked their mouse on the figure. The WindowButtonDownFcn callback would be invoked when the user clicks down to begin dragging the slider. There is also a WindowMotionFcn callback you could use to get the current x,y position of the mouse after the WindowButtonDownFcn has been called but before the WindowButtonUpFcn has been called.
From there, unless your slider is directly on the figure where you are getting your x,y coordinates from in the WindowMotionFcn you would need to do a coordinate transformation to go from figure units to panel units where your slider is positioned.
So definitely technically feasible with pure Matlab. Just difficult in implementation.

Robert Cumming
Robert Cumming 2015-3-27
You have 2 options (that I can currently think of)
1 - Using core Matlab - you need to combine the GUI mouse motion callback with you slider, the code below shows a schematic of how that can be done. The Window Mouse Motion callback is called whenever the mouse moves. If you check to see the current slider value is the same as the last time the mouse moved:
function activeSliderCallbackExample
d = figure;
uiH = uicontrol ( 'Style', 'slider', 'Parent', d, 'position', [50 50 300 100] );
uiH.UserData.lastValue = 0; % initialise the property
d.WindowButtonMotionFcn = @(obj,event)MouseMotionCallback(obj,uiH);
end
function MouseMotionCallback ( hFig, hUIC )
if hUIC.UserData.lastValue ~= hUIC.Value
disp ( 'slider moving' );
hUIC.UserData.lastValue = hUIC.Value;
end
end
The other option is to use a java slider which has the callback you want:
d = figure;
[jButton, hButton] = javacomponent('javax.swing.JSlider');
set(jButton,'StateChangedCallback',@(a,b)disp('state changed') )

Stephen23
Stephen23 2015-3-27
编辑:Stephen23 2015-3-29
Actually it is quite easy to do "realtime" updating of values while the slider is moving. I also faced this challenge and solved it for multiple sliders. You can see the working code here, in my submssion Mfile "cubehelix_view", which was written for 2010b:
The core of it sets a listener like this:
addlistener(h, 'Value', 'PostSet', @callbackfun);
The method I used uses some advice gleaned from various sources:

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by