How to display different images with slider?

9 views (last 30 days)
preeti
preeti on 1 May 2015
Edited: Adam on 2 May 2015
As I am new to matlab I wanted to know the working of slider in order to display output. I have created a parent panel and another panel having 3 images. As shown in code that parent panel is smaller than the child one.Child panel is the one which is having images that I want in output.With the help of slider user can view images by scrolling it. What should be included in code of callback for slider so that when slider is moved the child panel should shows different images ?
The code is here-
function varargout = slider(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @slider_OpeningFcn, ...
'gui_OutputFcn', @slider_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
function slider_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
% UIWAIT makes slider wait for user response (see UIRESUME)
function varargout = slider_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function slider1_Callback(hObject, eventdata, handles)
function slider1_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
As shown in 'slider.jpg' I want to show output images but with a horizontal slider.

Answers (1)

Adam
Adam on 1 May 2015
Do you want the images to move across one axes at a time (i.e. when you scroll one step shift each image left, losing the left-most one and adding a new one on the right) or do you want to replace all 3 images with the next 3?
Either way should be quite easy if you paramaterise your slider correctly.
In the former case you need your slider to have a 'Max' of n-2 (off the top of my head) for n images with a min of 1 of course. In the latter case you will want a 'Max' of ceil(n/3). Obviously the slider steps should be set appropriately.
In the callback you need you just need to get the value from the slider and round it to the nearest integer (personally I enforce integer steps on the slider itself when the user moves it so they cannot possibly select a non-integer value, but that requires more work).
Then just use that slider value to index into an array of images (or image filenames or however you store your images) together with the next 2 indices after it to get your 3 images.
I assume you know how to put the images on screen having retrieved the correct ones?
  3 Comments
preeti
preeti on 2 May 2015
In slider callback how to do indexing with images ? Can I get some short example to do this.
Adam
Adam on 2 May 2015
Edited: Adam on 2 May 2015
It all depends how your images are stored. But assuming they are in some array then just something like (in this example a cell array):
idx = round( get( handles.slider1, 'Value' ) );
im = imageArray{idx:idx+2};
imshow( im{1}, 'Parent', handles.axes1 );
imshow( im{2}, 'Parent', handles.axes2 );
imshow( im{3}, 'Parent', handles.axes3 );
Obviously it can be done a bit tidier than that and with index validation etc, but that is the general idea.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!