2

I created a MATLAB Gui using GUIDE. In that GUI I use sliders to set some parameters. I set some reasonable limit to the slider, but I want the user to be able to increase the value over that initial limit. For example the slider by default has the limits min 1 and max 10. If it is placed at 10 and the user clicks the arrow for increasing, I want to set a new value for max as 11.

To be able to do so, I want to specify the type of user interaction within the sliders' callback function. I want to check, whether the user clicked the increasing button on the slider, and if so, in case the slider is at max value, I want to change the max property.

Is there a way of getting this information about the user interaction?

I recognized that, if the slider is already the maximum value, and the user clicks the sliders' button for increasing, the sliders' callback isn't called. It seems, that the callback function is only called, if the slider is actually moved. So I would guess there are seperate callback function for the different buttons of the slider and I would need to access these.

T Ziehm
  • 23
  • 5

1 Answers1

2

As you found out yourself, the Matlab callback is not called when the value is already at the maximum (or minimum).

One way would be to retrieve the java handle of the slider, and act on the event that will be fired when you click the slider button, but this would be half way into the 'undocumented' functionalities and has a chance (albeit small in this case) of not being compatible in future releases.

A pure Matlab way to get round your problem is to use another event available from the slider, the KeyPressedFcn.

For example, you could decide that action with the mouse would only move the slider values between the set boundaries, but hitting + or - on the keyboard could override the maximum/minimum and reset them a bit further away.

This is implemented in this minimal example. Save the code below into a single slidertest.m file, then run it. Try to navigate to the min/max with the mouse, then with the + and - keys and see how it works. You should be able to implement more complex behaviour relatively simply if you need to.

Simple slider with expendable boundaries :

slidertest

function h = slidertest

    h.f   = figure('Position',[200 200 500 150],'Menubar','none') ;
    h.sld = uicontrol('style','slider','position',[20 20 460 30],...
                    'Min',0 , 'Max',10 , 'SliderStep',[0.01 0.1] , 'Value', 1 , ...
                    'Tooltip','Use the `+` and `-` keys to override min and max boundaries') ;
    h.txt = uicontrol('style','text','position',[20 80 460 40],'String','1','Fontsize',20) ;

    set(h.sld,'Callback', {@sld_callback,h} )       %// set the Callback function for the slider
    set(h.sld,'KeyPressFcn', {@sld_KeyPressFcn,h} ) %// set the KeyPress function for the slider


function sld_callback(hobj,~,h)
    val = get(hobj,'Value') ;
    set( h.txt,'String', num2str(val) )
    %// put here whatever code has to be executed when you change the slider value


function sld_KeyPressFcn(hobj,evt,h)

    minval = get(hobj,'Min') ;
    maxval = get(hobj,'Max') ;
    val    = get(hobj,'Value') ;

    keyIncrement = 1 ; %// define that to what suits you

    switch evt.Character
        case '+'
            %// check if we have to increase the 'Max' before we change the value
            if (val+keyIncrement) > maxval
                set( hobj , 'Max' , maxval+keyIncrement ) ;
            end
            %// increment the value
            set( hobj , 'Value' , val+keyIncrement ) ;

        case '-'
            %// check if we have to decrease the 'Min' before we change the value
            if (val-keyIncrement) < minval
                set( hobj , 'Min' , minval-keyIncrement ) ;
            end
            %// decrement the value
            set( hobj , 'Value' , val-keyIncrement ) ;

        otherwise
            %// if you think about other cases ...
    end
    %// this is called just to update the display
    %// in your case it would insure whatever callback code you have for the
    %// slider is executed with the new value
    sld_callback(hobj,[],h)
Hoki
  • 11,637
  • 1
  • 24
  • 43