Clear Filters
Clear Filters

Using a slider component

1 view (last 30 days)
Abdias
Abdias on 22 Oct 2022
Answered: Amit Dhakite on 16 Feb 2023
Hello! I am currently learning how to create an app with matlab and I need help with the slider component. I have a UIaxes created with data and now I want to use a slider to allow the user to pick a year (x axis) and have the graph highlight or show the Y axis. Thank you for your time and knowledge!
% Button down function: UIAxes
function UIAxesButtonDown(app, event)
app.UIAxes.XTickMode = 'auto';
app.UIAxes.YTickMode = 'auto';
app.UIAxes.XTickLabelMode = "auto";
app.UIAxes.YTickLabelMode = "auto";
x = 2000:1:2015;
y = [15963118.48, 16310353.16, 16597710.52, 16890900.28, 17188234.08, 17488388.63, 17790991.86, 18094962.78, 18691004.11, 18974535.08, 19245570.55 , 19504600.9 ,19752407.16 ,19990338.32 ,20219176.19, 21894356.15];
plot(app.UIAxes,x,y);
end
% Value changing function: Slider
function SliderValueChanging(app, event)
changingValue = event.Value;
end
end

Answers (1)

Amit Dhakite
Amit Dhakite on 16 Feb 2023
Hi Abdias,
As per my understanding, you want to make an app in which you can take input of the year from the slider, and highlight that year's part of the plot.
You can consider the attached file which contains an example of the implementation to achieve the required results.
% Value changed function: Slider
function ValueChange(app, event)
value = floor(app.Slider.Value);
app.Slider.Value = value;
x = app.X;
y = app.Y;
plot(app.UIAxes, x, y);
% You can edit the value of range to change the width of
% highlighted part
range = 1;
ptchidx = (x >= value - range) & (x <= value + range);
patch(app.UIAxes, [x(ptchidx) fliplr(x(ptchidx))], [y(ptchidx) zeros(size(y(ptchidx)))], [0.9 0.7 0.5], 'FaceAlpha',0.3, 'EdgeColor','none')
end
For further information on the functions used in the code, kindly refer to the following links:

Categories

Find more on Specifying Target for Graphics Output 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!