在 MATLAB GUI 中使用滑块 [英] Use a slider in MATLAB GUI

查看:97
本文介绍了在 MATLAB GUI 中使用滑块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很简单的问题.我希望用一个简单的 plot 创建 GUI ,它可以通过滑块改变一些东西.我一直在使用 GUI,并且在面板和 axies1 上有一个滑块+文本.所以对于初学者来说,我只想让滑块从 1:10(没有特定的增量)开始并缩放 y 值(按 1:10).我已将数据导入 GUI,因此省略了我拥有的通用自动生成代码:

Really simple question. I wish to create GUI with a simple plot that changes something with a slider. I have been using the GUI and have a slider+text on a panel and axies1. So for starters I just wish to have the slider going from 1:10 (no specific increments) and scaling the y-values (by 1:10). I have imported my data into the GUI, so leaving out the generic auto-generate code I have:

Graphslide_OpeningFcn 下

Under Graphslide_OpeningFcn

handles.OutAirTemp = OutAirTemp;
handles.SupAirTemp = SupAirTemp;
guidata(hObject,handles);
handles.a = get(handles.slider2,'Value');
plot(handles.SupAirTemp,handles.a*handles.OutAirTemp)

在slider2_Callback下

Under slider2_Callback

a = get(hObject,'Value')

很明显我错过了一些东西!任何指针、理论或代码都将受到极大的欢迎.

So clearly I am missing something! Any pointers, theory or code will be greatly received.

Edit1上述没有错误信息.然而,它在滑动滑块时并没有改变图形.

Edit1 There was no error message for the above. It however didn't change the graph when sliding the slider.

推荐答案

滑块回调是在您释放它后执行的回调.在上面的代码中,您需要更新您在滑块回调中的坐标区中绘制的值;你实际上很接近.我想移动这些线:

The slider callback is the one executed once you release it. In your code above, you need to update the values that you plot in the axes in the slider callback; you're pretty close actually. I think moving those lines:

handles.a = get(handles.slider2,'Value');
plot(handles.SupAirTemp,handles.a*handles.OutAirTemp)

在滑块回调内将执行您想要的操作.

inside the slider callback will do what you want.

顺便说一句,您可以查看以下代码以生成一个简单的 GUI,其中包含一个轴、一个滑块和一个显示滑块当前值的编辑框.您还可以设置滑块的属性,例如最小值、最大值和步长,以获得所需的行为.

As an aside, you can look at the following code to generate a simple GUI with an axes, a slider and an edit box in which the current value of the slider is displayed. You can set up the properties of the slider such as the min, max and step as well to get the behaviour you want.

function GUI_slider
clc
clear

%// Create GUI controls
handles.figure = figure('Position',[100 100 500 500],'Units','Pixels');

handles.axes1 = axes('Units','Pixels','Position',[60,100,400,300]);

handles.Slider1 = uicontrol('Style','slider','Position',[60 20 400 50],'Min',0,'Max',1,'SliderStep',[.1 .1],'Callback',@SliderCallback);

handles.Edit1 = uicontrol('Style','Edit','Position',[250 450 100 20],'String','Update Me');
handles.Text1 = uicontrol('Style','Text','Position',[180 450 60 20],'String','Slider Value');

handles.xrange = 1:20; %// Use to generate dummy data to plot.

guidata(handles.figure,handles); %// Update the handles structure.

    function SliderCallback(~,~) %// This is the slider callback, executed when you release the it or press the arrows at each extremity. 

        handles = guidata(gcf);

        SliderValue = get(handles.Slider1,'Value');
        set(handles.Edit1,'String',num2str(SliderValue));

        plot(handles.xrange,SliderValue*rand(1,20),'Parent',handles.axes1);
    end

end

希望有帮助!

这篇关于在 MATLAB GUI 中使用滑块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆