交互式情节 - 情节变化

时间:2015-03-17 17:08:45

标签: matlab user-interface matlab-figure

我正在尝试使用带有滑块的uicontrol创建一个GUI,该滑块将根据用户在数据上设置滑块的位置继续对数据集进行分区,如何执行此操作并使用用户设置的值进行修剪向下我的数据集并使用较小的数据集来完成我的其余功能?这是我到目前为止创建的

function myui

 MM = load('DATA.txt');
 X = MM(:,4);
  % Create a figure and axes
 f = figure('Visible','off');

 sldplot=plot(X)

  n = length(X);     


slider1 = uicontrol('Style', 'slider','Min',1,'Max',n,'Value',n,'Position', [100 20 120 20],'Callback', @slider1_Callback);

slider2 = uicontrol('Style', 'slider','Min',1,'Max',n,'Value',1,'Position', [400 20 120 20],'Callback', @slider2_Callback);
txt = uicontrol('Style','text','Position',[400 45 120 20],'String','Second cut');
txt2 = uicontrol('Style','text','Position',[100 45 120 20],'String','First cut');
addlistener(slider1, 'ContinuousValueChange', @myCallbackFcn1)
addlistener(slider2, 'ContinuousValueChange', @myCallbackFcn2)

    function myCallbackFcn1(source,callbackdata)

n2 = get(slider1,'Value');
n3 = get(slider2, 'Value');

plot(X(n2:n3))
set(sldplot,'xdata',X(n2:n3));

drawnow;
    end
    f.Visible = 'on';

    function slider1_Callback(source,callbackdata)
        val = get(slider1,'Value');

        Xnew = X(val:n);

    end

    function slider2_Callback(source,callbackdata)
        val2 = get(slider2,'Value');


        Xnew2 = X(1:val2);
    end


 end
然而,这似乎没有做我想要的。我得到了我的数据的静态图,滑块似乎没有移动或放大数据

1 个答案:

答案 0 :(得分:0)

你几乎拥有它。在回调中,您实际上不需要再次调用plot,因为您在函数开头就掌握了图表的句柄。您需要做的只是更新显示的x和ydata。

我做了一些细微的改动,现在工作正常。我重命名了监听器回调,为它们提供了更多信息,并使用虚拟数据进行绘图。您需要根据自己的数据进行调整,但这并不难。我还交换了两个滑块的初始值。

这是代码。我在添加/修改内容的地方添加了%// === NEW ===

function myui
clear
clc
close all

%// Uncomment this and adjust the rest to fit with your data
%MM = load('DATA.txt');
%X = MM(:,4);

X = rand(1,100);

% Create a figure and axes
f = figure('Visible','off');

%// == NEW ==
%// Create an axes
hAxes = axes('Position',[.1 .15 .8 .8]);
sldplot=plot(X);

%// == NEW ==
%// Get axis limits to make sure the limits always stay the same
x_limit = get(gca,'XLim');
y_limit = get(gca,'YLim');

n = numel(X);

%// == NEW ==  I changed the values of the sliders
slider1 = uicontrol('Style', 'slider','Min',1,'Max',n,'Value',1,'Position', [100 5 120 20],'Callback', @slider1_Callback);

slider2 = uicontrol('Style', 'slider','Min',1,'Max',n,'Value',n,'Position', [400 5 120 20],'Callback', @slider2_Callback);
txt = uicontrol('Style','text','Position',[400 30 120 15],'String','Second cut');
txt2 = uicontrol('Style','text','Position',[100 30 120 15],'String','First cut');

%// ======== NEW ========

button1 = uicontrol('Style','push','position',[260 20 60 20],'String','Get Xnew','Callback',@ButtonCallback)

%// =====================

addlistener(slider1, 'ContinuousValueChange', @Slider1ListenerCallback);
addlistener(slider2, 'ContinuousValueChange', @Slider2ListenerCallback);
set(f,'Visible','on');

    function Slider1ListenerCallback(source,callbackdata)

        n2 = round(get(slider1,'Value'));
        n3 = round(get(slider2, 'Value'));

        %// == NEW ==
        %// Updata xdata only. DO NOT plot again
        set(sldplot,'xdata',(n2:n3),'ydata',X(n2:n3));

        %// == NEW ==
        set(gca,'XLim',x_limit,'YLim',y_limit)
        drawnow;
    end


    function slider1_Callback(source,callbackdata)
        n2 = round(get(slider1,'Value'));
        n3 = round(get(slider2, 'Value'));

        %// Updata xdata and ydata only. DO NOT plot again
        set(sldplot,'xdata',(n2:n3),'ydata',X(n2:n3));

        set(gca,'XLim',x_limit,'YLim',y_limit)
        drawnow;

    end


    function Slider2ListenerCallback(source,callbackdata)

        n2 = round(get(slider1,'Value'));
        n3 = round(get(slider2, 'Value'));

        %// Updata xdata and ydata only. DO NOT plot again
        set(sldplot,'xdata',(n2:n3),'ydata',X(n2:n3));

        set(gca,'XLim',x_limit,'YLim',y_limit)
        drawnow;
    end


    function slider2_Callback(source,callbackdata)
        n2 = round(get(slider1,'Value'));
        n3 = round(get(slider2, 'Value'));

        %// Updata xdata and ydata  only. DO NOT plot again
        set(sldplot,'xdata',(n2:n3),'ydata',X(n2:n3));

        set(gca,'XLim',x_limit,'YLim',y_limit)
        drawnow;

    end

%// ======== NEW ========
    function ButtonCallback(source,callbackdata)
        n2 = round(get(slider1,'Value'));
        n3 = round(get(slider2, 'Value'));

        Xnew = X(n2:n3)
    end
%// =====================
end

移动两个滑块后的截图:

enter image description here

为了获得XNew,我添加了一个按钮。在函数的最后Xnew = X(n2:n3)之前放置end会导致错误,因为启动函数时未定义n2n3,因为Matlab从顶部读取所有代码底部。因此,按下按钮以获得值更安全。