MATLAB 2015 slider gui question
조회 수: 5 (최근 30일)
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.
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 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:
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);
function MouseMotionCallback ( hFig, hUIC )
if hUIC.UserData.lastValue ~= hUIC.Value
disp ( 'slider moving' );
hUIC.UserData.lastValue = hUIC.Value;
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 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: