필터 지우기
필터 지우기

Using a slider component

조회 수: 1 (최근 30일)
Abdias
Abdias 2022년 10월 22일
답변: Amit Dhakite 2023년 2월 16일
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