Use a slider in MATLAB GUI

南楼画角 提交于 2021-02-11 12:15:27

问题


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:

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)

Under slider2_Callback

a = get(hObject,'Value')

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

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


回答1:


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.

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

Hope that helps!




回答2:


You don't say what error message you get or what the problem is, so we can't guess what's wrong. However, there are a few things I can see are wrong:

  • you probably need to specify the handle of the axes on which to plot when issuing the plot command
  • for your slider2_Callback, you probably need

    handles.a = get(hObject,'Value') guidata(hObject,handles);

  • for your Graphslide_OpeningFcn, you don't say where OutAirTemp and SupAirTemp come from. I would also issue the guidata at the end of the function rather than in the middle as your are doing.



来源:https://stackoverflow.com/questions/26632952/use-a-slider-in-matlab-gui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!