MATLAB - 添加校准滑块到图形

MATLAB - adding calibrated slider to figure

我正在尝试向该图添加校准滑块。 它是一个带有 3d 冲浪图的图形框架。

现在如您所见,我设法使用“uicontrol”命令创建了一个滑块,但我无法对其进行校准,也无法在其上创建任何刻度。

我尝试使用“uislider”,它创建了一个漂亮的滑块,就像我想要的那样,但出于某种原因,我无法在图中添加其中一个(但是只有当我使用 uifigure 创建原始图形时才有效)当我这样做时,我无法在其中绘制我的冲浪图,我也不知道为什么)。

这是我的代码。

主脚本。

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);

脚本 getCylinderHeatMap 并不重要,因为它只是 returns 一个包含用于创建圆柱体的值的矩阵。

圆柱体创建脚本。

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

请提供任何帮助,因为我已经坚持了 2 天了。

MATLAB GUI uislider() 带捕捉点

使用回调函数绘制的随机测试数据,下图是滑块值变化时绘制的。可以在这段代码中的回调函数 Snap_Slider() 中获取滑块值,图中的绘图和图像可以随意更新。除了滑块之外,我建议添加 uieditfield 以获得更精确的 image/plot 选择。可以在任何预期由用户或其他代码修改的元素上调用回调函数。可以根据需要调用回调函数。在这种情况下,它是在值更改时由附加到元素的 .ValueChangedFcn 事件指示的,在这种情况下,uislider 名为 Slider。可以按以下形式创建回调:

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

回调函数中的输入也是可以接受的,可以包含其他 UI(用户界面)元素。

Components/Elements包括:

图→uifigure()(父容器)
情节 → uiaxes()
图片 → uiimage()
滑块标签 → uilabel()
滑块 → 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

运行 使用 MATLAB R2019b