matlab slider max,MATLAB - adding calibrated slider to figure

博主尝试在MATLAB中为3D表面图添加一个校准的滑块控制器,但遇到困难。他们成功创建了滑块,但无法校准它或添加刻度。尝试使用`uislider`函数创建理想的滑块,却发现只能在`uifigure`中使用,而不能在`figure`中使用,这导致无法同时显示3D表面图。代码示例展示了创建滑块和3D图的过程。建议添加编辑框以精确选择图像,并提供了滑块更改值时更新图形的回调函数示例。
摘要由CSDN通过智能技术生成

原文:

I am trying to add a calibrated slider to this figure.

its a figure frame with a 3d surf plot in it.

x2BKO.jpg

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

4d350fd91e33782268f371d7edaa8a76.png

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

1e3f58dd8e1348a15dabe93eb5fcb644.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值