Search code examples
matlabmatlab-gui

How to make an object current in GUI matlab?


I build a function "sng_figureslide" which spawns a slider that can slide through all figures that are already created. I have to click on de slider to be able to control it with the arrow keys.

I'm wondering if it is possible to make the slider-object active when I call the function so i don't have to click on the slider in advance.

function sng_figureslide

figh=findobj('type','figure');
figs=numel(figh);

% if there are one or zero figures, the slider does not appear
if figs > 1
    sliderstep = 1/(figs-1);
else
    return
end

fignumber = cell2mat(get(figh,'Number'));
sliderfignumber = max(fignumber) + 1;
figure(max(fignumber) +1)
set(gcf,'position',[360 190 560 36]);
set(gcf,'Toolbar','none');
a=annotation('textbox',[0.03 0.2 0.1 0.7],'String',num2str(figs));

uicontrol('Style', 'slider',...
    'Min',1,'Max',figs,'Value',figs,...  
    'Position', [80 10 470 20],...  
    'Callback', {@slider1,figh,a,figs,fignumber,sliderfignumber},...      
    'SliderStep', [sliderstep sliderstep]);
end

function slider1(hObj,~,figh,a,figs,fignumber,sliderfignumber)
    Val = round(get(hObj,'Value'));
    set(a,'String',num2str(Val));
    figure(figh(Val));
    figure(sliderfignumber);
end

Besides, i think this function is pretty useful for many people so feel free to use it.


Solution

  • When you create the uicontrol save the handle to it. Then pass that handle back to the uicontrol function.

    h = uicontrol ( .... )
    uicontrol ( h );