How can I use sliders to scroll through my data in GUI?

Hi all!
Currenlty, I am building an GUI for loading patient data using App Designer in Matlab. First of all, a patient needs to be chosen, which I got working.
Next, the data set linked to the patient gets plotted, and shows vestibular pressure, abdominal pressure and detrusor pressure.
I want to use the slider below in the picture to make an indicator/dot scroll through the data and show in the Gauge meter if the pressure at a certain time is good/bad.
All tabs look the same - meaning a slider, a gauge and pressure data.
I would like the sliders to be connected, meaning if I drag one slider, the others move as well.
Currently, I used a callback function to show the time of the slider in a box next to the slider.
My two questions now are:
  • How do I use callbacks to connect all sliders, so the show the same value?
  • How do I add an indicator/dot in the plot indicating the chosen time value with the slider in the data?
Thank you.

 Accepted Answer

How do I use callbacks to connect all sliders, so they show the same value?
You can assign the same callback function to multiple sliders. When you right-click the slider object from the AppDesigner user interface, instead of selecting "Add .... callback", select "Select existing callback".
Within the shared callback function, all of the slider values should be updated whenever one of them is changed.
How do I add an indicator/dot in the plot indicating the chosen time value with the slider in the data?
1) Assuming the slider value represents the x-value within the plot, find the nearest x-value in the data to the slider value. The example below assumes that the red line is the first object listed as a child of your UIAxes. Otherwise, you'll need to index 'Children' differently.
xData = app.UIAxes.Children(1).XData;
yData = app.UIAxes.Children(1).YData;
% Find closest x-value to the slider value
[~, minIdx] = min(abs(xData - sliderValue));
2) Update the position of the point along the red line. If the point doesn't exist, create it. Notice the use of Tag to identify the point's handle.
% Find previous point, if it exists
previousPoint = findobj(app.UIAxes, 'tag', 'CurrentSlideValue');
if isempty(previousPoint)
% Point doesn't exist; create it.
plot(app.UIAxes, xData(minIdx), yData(minIdx), 'ko', 'MarkerFaceColor', 'b', 'tag', 'CurrentSlideValue')
else
% Update position of point
previousPoint.XData = xData(minIdx);
previousPoint.YData = yData(minIdx);
end

4 Comments

Dear Adam,
Thank you very much for you answer!
It works for the first data point I select. When I plot the data and chose a slider value, it does plot a dot with the correct x and y value.
However, the red line disappears in the app.UIAxes, now only showing the blue dot.
This means that the xData and yData now become that dot, and plotting this dot for every slider value I select.
How would you think I can plot both my red data graph and the blue dot in the same graph?
Using hold( handles.UIAxes, 'on' ) will plot every blue dot ever created on top of the graph.
Thank you in advance!
"However, the red line disappears in the app.UIAxes, now only showing the blue dot."
I have a feeling this can be fixed by adding the following line to you code so that it is executed prior to adding the blue dot. It only needs to be executed once (although I won't cause problems if it's executed more than once, it's just unnecessary).
hold(app.UIAxes, 'on')
"This means that the xData and yData now become that dot, and plotting this dot for every slider value I select." & "Using hold( handles.UIAxes, 'on' ) will plot every blue dot ever created on top of the graph."
It's not true that hold on will plot every blue dot created. In fact, only one blue dot is created in my answer. Step #2 in my code replaces the x and y values of the blue dot, it does not plot another blue dot.
However, if you find that the first step of my answer is not selecting the correct line object, you could assign the red line a Tag value so that it can be identified as I've shown in step 2.
Thank you very much! Everything seems to be working now.
Great! Thanks for the feedback.

Sign in to comment.

More Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!