MATLAB – adding calibrated slider to figure

MATLAB GUI uislider() with Snapping Points

With random test data plotted using the callback function, the following figure is plotted when the slider value is changed. The slider value can be obtained within the callback function Snap_Slider() within in this code the plots and images in the figure can be updated as pleased. I’d suggest adding a uieditfield for more precise image/plot selection in addition to the slider. A callback function can be called on any element that expects to be modified by the user or other code. A callback function can be called depending on the case required. In this case, it is when the value is changed indicated by .ValueChangedFcn event attached to the element in this case the uislider named Slider. A callback can be created in the form:

Slider.ValueChangedFcn = @(Slider,event) Callback_Function();

Inputs in the callback function are also acceptable and can include other UI (User Interface) elements.

Components/Elements Include:

Figure → uifigure() (parent container)
Plot → uiaxes()
Image → uiimage()
Slider Label → uilabel()
Slider → uislider()

Slider UIFigure

clf;
clear;
close all;
clc;

%Figure/parent container (uifigure) properties%
App = uifigure('Scrollable','on','Name','Heatmap Plots','NumberTitle','off');
App_Width = 1000; App_Height = 500;
App.Position = [0 0 App_Width App_Height];

%Slider label (uilabel) properties%
Slider_Label = uilabel('Parent',App);
Slider_Label.Text = "Cylinder Number";
Slider_Label.Position = [25 20 200 100];

%Slider (uislider) properties%
Slider = uislider('Parent',App);
Slider.Limits = [1 1000];
Slider.Value = 1;
Slider_Width = App_Width - 50;
Margin = (App_Width - Slider_Width)/2;
Slider.Position = [Margin 50 Slider_Width 3];
Slider.MajorTicks = (1:100:1000);
Slider.FontSize = 6;
Red = 87; Green = 207; Blue = 220;
Slider.FontColor = [Red/255 Green/255 Blue/255];

%Plot (uiaxes) properties%
Heatmap_Cylinder_Plot = uiaxes('Parent',App);
Heatmap_Cylinder_Plot_X_Position = 100;
Heatmap_Cylinder_Plot_Y_Position = 100;
Heatmap_Cylinder_Plot_Height = 350;
Heatmap_Cylinder_Plot_Width = 400;
Heatmap_Cylinder_Plot.Position = [Heatmap_Cylinder_Plot_X_Position Heatmap_Cylinder_Plot_Y_Position Heatmap_Cylinder_Plot_Width Heatmap_Cylinder_Plot_Height];
Heatmap_Cylinder_Plot.GridColor = [0.15 0.15 0.15];
Heatmap_Cylinder_Plot.XGrid = 'on';
Heatmap_Cylinder_Plot.YGrid = 'on';
Heatmap_Cylinder_Plot.ZGrid = 'on';

%Image (uiimage) properties%
Heatmap_Image = uiimage('Parent',App);
Heatmap_X_Position = (App_Width/2) + 50;
Heatmap_Y_Position = 80;
Heatmap_Height = 350;
Heatmap_Width = 400;
Heatmap_Image.Position = [Heatmap_X_Position Heatmap_Y_Position Heatmap_Height Heatmap_Width];

%Callback function as the slider is moved%
Slider.ValueChangedFcn = @(Slider,event) Snap_Slider(Slider,Slider_Label,Heatmap_Cylinder_Plot,Heatmap_Image);


%Callback function definition%
function [] = Snap_Slider(Slider,Slider_Label,Heatmap_Cylinder_Plot,Heatmap_Image)
Slider_Value = Slider.Value;
Slider.Value = round(Slider.Value);

Slider_Label.Text = "Cylinder Number: " + num2str(Slider.Value);

fprintf("Plotting figure %d\n",Slider.Value);
%Put plotting code here%
plot(Heatmap_Cylinder_Plot,[1 2 3 4 5 6 7]);

%Put image plotting code here%
Heatmap_Image.ImageSource = 'peppers.png';




end

Ran using MATLAB R2019b

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top