本文主要是介绍matlab slider max,MATLAB - adding calibrated slider to figure,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原文:
I am trying to add a calibrated slider to this figure.
its a figure frame with a 3d surf plot in it.
Now as you can see I managed to create a slider with the "uicontrol" command but i can not calibrate it nor created any ticks on it.
I tried using the "uislider" which creates a beautiful slider just like the one I want but for some reason I cant cant add one of those in a figure (only works if I use uifigure to create the original figure however when I do this I cant plot my surf plot in it and I don't know why).
This is my code.
Main script.
clear vars
filename = ('C:\Users\Ali\Desktop\Documents\DataVis\Projekt\data\day\filenames.txt');
%This line simply gives us a table of all filenames in this file.
T = readtable(filename);
tsize = size(T);
tsize2 = size(T, 1);
%initialize figure.
mainFigure = figure('name','CylinderHeatMap','NumberTitle','off','Color',[.3 .3 .3]);
%extracts the content of the table as a categorical array.
% {rownumber,variablenumber}. {100,1} = row 100, variable 1.
%converts the categorical array into a string array.
%joins the string array across the column.
%string(T{100:105,1}); implies from row 100 to row 105 and use variable 1.
%This line simply adds the name of the file at row 100 to the path of the
%file. Hnece we get a full filepath.
filename = strcat('\Users\Ali\Desktop\Documents\DataVis\Projekt\data\day\', string(T{100,1}));
map100 = getCylinderHeatMap(filename);
roterOven = createSurfCylinder(map100);
s = uicontrol('Style','Slider','Parent',mainFigure,...
'Units','normalized','Position',[0 0 1 .025],...
'Value',1,'Callback',{@slider_callback1}, 'min', 1, 'max', 1000);
the script getCylinderHeatMap is not of any importance as it only returns a matrix with values to create the cylinder.
Cylinder creation script.
function cylinder = createSurfCylinder(matrix)
%Load heat map.
load('myHeatMap.mat','myHeatMap');
%%
%Cylinder creation
Sample_Range = 255 - 0;
Temperature_Range = 450 - 50;
Multiplier = Temperature_Range/Sample_Range;
map100 = matrix.*Multiplier + 50;
%Setting up the figure%
Radius = 1.5;
Number_Of_Data_Points = 360;
theta = linspace(0,2*pi,Number_Of_Data_Points);
%The xy values according to radius and number of points%
Z_Circle = Radius*cos(theta);
Y_Circle = Radius*sin(theta);
map100 = rot90(map100);
Height = 512;
Z_Circle = repmat(Z_Circle,Height,1);
Y_Circle = repmat(Y_Circle,Height,1);
X_Length = (1:512)';
X_Length = repmat(X_Length,1,Number_Of_Data_Points);
figure('Position', [10 10 800 500])
clf;
close;
%surf(X_Circle,Y_Circle,Z_Height,'Cdata',map100); vertical
%subplot(1,3,1:2);
cyl = surf(X_Length,Y_Circle,Z_Circle,'Cdata',map100);
title("3D Heatmap Plot");
zlabel("Z-Position");
ylabel("Y-Position");
xlabel("Length(Cm)");
%Reverse Y axis.
set(gca,'Ydir','reverse')
colormap(myHeatMap);
colorbar;
shading interp
Maximum_Value = 450;
Minimum_Value = 50;
caxis([Minimum_Value Maximum_Value]);
%Show the image in the subplot and add custome color coding to it.
% subplot(1,3,3); imshow(rot90(map100));
% colormap(myHeatMap);
% caxis([Minimum_Value Maximum_Value]);
cylinder = cyl;
%%
end
Please any help would be much appreciated as I have been stuck on this for 2 days now.
# Answer 1
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()
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
这篇关于matlab slider max,MATLAB - adding calibrated slider to figure的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!