How to autoscroll slider in this case using pushbutton?

5 views (last 30 days)
Example: (Here I am enclosing the another example. When we use slider it changes the images. I wanted to insert a button that can automatically move the slider.)
I have created GUI which has slider . When Slider moves manually it will show images from the folder. But now I wanted to add a pushbutton which can automate the slider function. Can you give some idea for this?

Accepted Answer

Geoff Hayes
Geoff Hayes on 9 Jun 2022
@Swetha C - you may want to nest your code within a main function so that you don't have to use the @doc:guidata which may not be appropriate for your GUI (i.e. might not always work). This would mean your code becomes
%Load an example image
function untitled5
a=imread('matlab.jpg');
%Replicate 100 times
database=repmat(a,1,1,100);
N_images=size(database,3);
%prepare figure and guidata struct
h=struct;
h.f=figure;
h.ax=axes('Parent',h.f,...
'Units','Normalized',...
'Position',[0.1 0.1 0.6 0.8]);
h.slider=uicontrol('Parent',h.f,...
'Units','Normalized',...
'Position',[0.8 0.1 0.1 0.8],...
'Style','Slider',...
'BackgroundColor',[1 1 1],...
'Min',1,'Max',N_images,'Value',1,...
'Callback',@sliderCallback);
%store image database to the guidata struct as well
h.database=database;
%trigger a callback
sliderCallback(h.slider)
function sliderCallback(hObject,eventdata)
count=round(get(hObject,'Value'));
IM=h.database(:,:,1:count);
IM=permute(IM,[1 2 4 3]);%montage needs the 3rd dim to be the color channel
montage(IM,'Parent',h.ax);
end
end
Within the above you would add the code to create the button and add its callback
h.button=uicontrol('Parent',h.f,...
'Units','Normalized',...
'Position',[0.8 0.1 0.1 0.1],...
'Style','pushbutton',...
'BackgroundColor',[1 1 1],...
'String', 'Next image', ...
'Callback',@buttonCallback);
function buttonCallback(hObject,eventdata)
% get the current slider value
sliderValue = get(h.slider, 'Value');
% if slider value is less than the maximum, then we can increment
if sliderValue < (get(h.slider, 'Max') - get(h.slider, 'Min'))
% update the slider
set(h.slider, 'Value', sliderValue + 1);
drawnow;
% call the slider callback
sliderCallback(h.slider, []);
end
end
You would have similar code to go in the other direction (i.e. previous image).

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!