How to autoscroll slider in this case using pushbutton?

1 次查看(过去 30 天)
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?

采纳的回答

Geoff Hayes
Geoff Hayes 2022-6-9
@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).

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by