How to control the content of a plot using a slider preferably without GUIDE

8 views (last 30 days)
I would like to build a simple GUI that controls the content of a plot. More specifically, I would like to control the index of an array with a slider. The values of the slider has to be integers.
To plot one layer of the map is fairly easy :
map=rand(100,100,21);
figure(1);clf;
i=1 %first layer.
imagesc(map(:,:,i));
Ideally, I would like to add a slider that updates the value if 'i' automatically.

Answers (1)

Adam
Adam on 3 Nov 2015
Edited: Adam on 3 Nov 2015
function sliderTest( )
hFig = figure;
hText = uicontrol( hFig, 'Style', 'text', 'String', 5, 'Position', [100 20 20 20] );
hSlider = uicontrol( hFig, 'Style', 'slider', 'Min', 1, 'Max', 10, 'Value', 5, 'Callback', @(src,evt) sliderCallback( src, hText ) );
function sliderCallback( hSlider, hText )
idx = round( hSlider.Value );
hText.String = num2str( idx );
gives a very basic example of having a slider update another uicomponent with integers only (in this case a text control).
Obviously in your case you would need to pass information to your callback that enables it to update your images.
(Edit)
So in your case you would want something more like:
function somethingOrOther( )
map=rand(100,100,21);
figure(1);clf;
i=1 %first layer.
hImage = imagesc(map(:,:,i));
callback = @(src,evt) sliderCallback( src, hImage, map );
hSlider = uicontrol( ..., 'Callback', callback );
function sliderCallback( hSlider, hImage, map )
idx = round( hSlider.Value );
hImage.CData = map(:,:,idx);
That is a basic framework. You may want to adjust aspects of the image in some way, etc, but hopefully that gets you started.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!