This is the function I've used to do something like this with some control over the figure properties for some time now:
function genCustomSlider(plotterfcn,Data,varargin)
fig=figure;
set(fig,'Toolbar','figure','NumberTitle','off')
% Create an axes to plot in
ax = axes('Position',[0.1300 0.1500 0.7750 0.8150]);
% sliders for epsilon and lambda
len = size(Data,ndims(Data));
slider1_handle=uicontrol(fig,'Style','slider','Max',len,'Min',1,...
'Value',1,'SliderStep',[1/(len-1) 10/(len-1)],...
'Units','normalized','Position',[.02 .02 .9 .05]);
label = uicontrol(fig,'Style','text','Units','normalized','Position',[.02 .07 .14 .04],...
'String','Choose frame');
% Set up callbacks
% climVals = [mean(min(Data,[],[1,2])),mean(max(Data,[],[1,2]))];
vars=struct('slider1_handle',slider1_handle,'Data',Data,'Axes',ax,'Label',label,'Figure',fig);%,'CLIMS',climVals);
if nargin > 2
for i = 1:length(varargin)
newField = "field"+num2str(i);
vars.(newField) = varargin{i};
end
end
set(slider1_handle,'Callback',{@slider1_callback,vars,plotterfcn});
plotterfcn(vars);
% End of main file
end
% Callback subfunctions to support UI actions
function slider1_callback(~,~,vars,plotterfcn)
% Run slider1 which controls value of epsilon
plotterfcn(vars);
end
% Example of how plotterfcn may be written in source file:
% function plotterfcn(vars)
% % Plots the image
% val = round(get(vars.slider1_handle,'Value'));
% imagesc(vars.Axes,vars.Data(:,:,val));
% colormap(vars.Axes,'gray');
% set(vars.Label,'String',num2str(val));
% end
% Example of how to pass the function:
% genCustomSlider(@plotterfcn2,frame)
Basically, you save this file somewhere on your matlab path. Then inside your script, you create a version of the commented out function. Then you call genCustomSlider in your script and pass it the function handle.
If you want to add other variables that are inputs to genCustomSlider() and are usable inside the subfunction (i.e. axes values) then just add them as parameters to the function above and they will be incorporated in the if statement after the vars structure is defined. A simpler version of the code without that is the following:
function genSliderV2(Data)
fig=figure;
set(fig,'Toolbar','figure','NumberTitle','off')
% Create an axes to plot in
ax = axes('Position',[0.1300 0.1500 0.7750 0.8150]);
% sliders for epsilon and lambda
len = size(Data,3);
slider1_handle=uicontrol(fig,'Style','slider','Max',len,'Min',1,...
'Value',1,'SliderStep',[1/(len-1) 10/(len-1)],...
'Units','normalized','Position',[.02 .02 .9 .05]);
label = uicontrol(fig,'Style','text','Units','normalized','Position',[.02 .07 .14 .04],...
'String','Choose frame');
% Set up callbacks
% climVals = [mean(min(Data,[],[1,2])),mean(max(Data,[],[1,2]))];
vars=struct('slider1_handle',slider1_handle,'Data',Data,'Axes',ax,'Label',label);%,'CLIMS',climVals);
set(slider1_handle,'Callback',{@slider1_callback,vars});
plotterfcn(vars)
% End of main file
end
% Callback subfunctions to support UI actions
function slider1_callback(~,~,vars)
% Run slider1 which controls value of epsilon
plotterfcn(vars)
end
function plotterfcn(vars)
% Plots the image
val = round(get(vars.slider1_handle,'Value'));
imagesc(vars.Axes,vars.Data(:,:,val));
colormap(vars.Axes,'gray');
set(vars.Label,'String',num2str(val));
end