Make GUI Slider behave like iOS frame scroller

It is my understanding that the MATLAB GUI Slider cannot be made to behave like a frame scrubber. Suppose I have a Slider control that is used to control which frame of a video sequence is rendered on an Axes control, and I drag the Slider knob (the thing in the slider bar that moves left and right or up and down), it will not update the Value property (which tells you the current position of the knob in the Slider ) until the left mouse button is released.
Does anyone know if there is a way to make the Slider control in MATLAB's GUI facilities behave like the iOS frame scrubber?

 Accepted Answer

John - you can try implementing a continuous value change listener for your slider. That way, as the slider "button" moves, you can capture each value change. Assuming that you are using GUIDE, in your GUI, do the following: in the yourGuiName_OpeningFcn, create a listener as
function yourGuiName_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for untitled
handles.output = hObject;
% create the listener for the slider
handles.sliderListener = addlistener(handles.slider1,'ContinuousValueChange', ...
@(hFigure,eventdata) slider1ContValCallback(...
hObject,eventdata));
% update handles structure
guidata(hObject, handles);
In the above we create a listener for the slider (named slider1) which will fire the slider1ContValCallback callback on any change to its value. This callback takes two input parameters - the handle to the figure/GUI, hFigure, and the event data, eventdata (which we won't need, but a second input parameter is required).
Now we add the callback slider1ContValCallback as
function slider1ContValCallback(hFigure,eventdata)
% test it out - get the handles object and display the current value
handles = guidata(hFigure);
fprintf('slider value: %f\n',get(handles.slider1,'Value'));
The above callback simply gets the handles data (so all widget handles and user-defined data) from the figure handle using guidata. We then write out the current value of the slider.
Try the above for either moving the slider with the arrow buttons or manually moving the slider button up or down.
NOTE there will probably be a callback defined already for slider1 as slider1_Callback. This should be removed from the m-file and from the slider itself (because you don't need this callback and the continuous value change callback to both fire). In the GUI, again assuming GUIDE is being used, launch the Property Inspector for the slider and clear the text for the Callback property. The text will be something like
@(hObject,eventdata)yourGuiName('slider1_Callback',hObject,eventdata,guidata(hObject))
Close the inspector, and save. Try the above and see what happens!

7 Comments

Your example does work. A technique based on this should suit my needs. Thank you Geoff
I used MATLAB Compiler to package an app that used a slider that used the continuous value callback. The continuous callback works when the app is launched within the MATLAB environment, but when packaged with MATLAB Compiler and installed as a standalone with the MATLAB runtime, it does not work as expected; the slider callback is called when the slider is dropped to a new location but not while it's being moved.
Any ideas why that feature does not work when the app is packaged with Compiler? I could not find any MATLAB documentation on this. I used MATLAB Compiler for 2014a.
John - I've never used the MATLAB compiler so can't comment on why it wouldn't work.
John, one thing I've seen happen in compiled UIs but not in regular MATLAB is that the events can stack up (you're firing perhaps thousands of events moving the slider!) Because they stack up this can cause a lag that will not behave like you expect.
Try putting a drawnow inside of the callback the listener is calling. This will flush the event queue.
Hi Geoff. Thanks for your code, it helped me a lot. I'm using 4 sliders for 4 edit-text features. I have set everything ok but I have a problem with the Value. When I load the data into the edit-text the numbers are displayed fine, but when I then use the slider, it works fine just for the first one and not for the others. Example.. values for the variables are:
  • v1 = 8;
  • v2 = 5;
  • v3 = 6;
  • v4 = 10;
when I increase/decrease the number it works fine for v1 (goes to 9/7) but e.g. for v3 it goes 9/7 again! How can I fix this? Thanks.
giacomo - you would need to show your code so that we can get an idea of how it works and why you might bee seeing this behaviour.
ok sure.
EDIT: I attached the whole code so it's easier for you to check it. The four variables seem to be loaded fine, then when I change their values with the slider the program gives me errors, saying that all the variables aren't defined (?). In the last function (done_Callback) I tried to use getappdata to store the final edited v1 v2 v3 v4 values back into the workspace. I think the sintax is correct but I can't know if that's the case since I get the undefined variables error before. Thanks.
EDIT2: I also tried to use UIGETVARIABLES (https://es.mathworks.com/matlabcentral/fileexchange/37679-uigetvariables--dialog-to-pass-variables-from-workspace-into-gui) to avoid using evalin, but calling this in the GUI pushbutton9_Callback function:
tvar = uigetvariables({'Pick a scalar:','Pick a scalar:','Pick a
scalar:','Pick a scalar:'}, ...
'InputDimensions',[0 0 0 0]);
v1 = set(handles.value1,'String',num2str(cell2mat(tvar(1))));
%and same for the other 3 variables
prints the variables in the edittext but then when I use the sliders it gives me error: undefined variable v1 etc

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products

Asked:

on 7 Sep 2014

Edited:

on 16 Nov 2017

Community Treasure Hunt

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

Start Hunting!