Using a slider component

1 次查看(过去 30 天)
Abdias
Abdias 2022-10-22
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

回答(1 个)

Amit Dhakite
Amit Dhakite 2023-2-16
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:

类别

Help CenterFile Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by