互动情节——情节变化
Interactive plot - plot changing
我正在尝试使用带有滑块的 uicontrol 创建一个 GUI,它将继续根据用户在数据上设置滑块的位置来分割我的数据集,我该如何做到这一点并使用用户设置的值到 trim 下我的数据集并为我的其余函数使用较小的数据集?这是我到目前为止创建的
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
然而,这似乎不是我想要的。我得到了数据的静态图,滑块似乎没有移动或放大数据
你快搞定了。在回调中,您实际上不需要再次调用 plot
,因为您在函数的开头就获得了绘图的句柄。您需要做的只是更新显示的 x 和 y 数据。
我做了一些小改动,现在可以正常使用了。我重命名了侦听器回调以给它们提供更多信息的名称,并使用虚拟数据进行绘图。您将需要根据自己的数据进行调整,但这并不难。我还交换了两个滑块的初始值。
这是代码。我在 added/modified 东西的地方添加了 %// === 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
以及移动两个滑块后的屏幕截图:
为了得到XNew
我添加了一个按钮。将 Xnew = X(n2:n3)
放在函数的最后一个 end
之前会导致错误,因为启动函数时既没有定义 n2
也没有定义 n3
,因为 Matlab 从头到尾读取所有代码底部。因此按一个按钮获取值更安全。
我正在尝试使用带有滑块的 uicontrol 创建一个 GUI,它将继续根据用户在数据上设置滑块的位置来分割我的数据集,我该如何做到这一点并使用用户设置的值到 trim 下我的数据集并为我的其余函数使用较小的数据集?这是我到目前为止创建的
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
然而,这似乎不是我想要的。我得到了数据的静态图,滑块似乎没有移动或放大数据
你快搞定了。在回调中,您实际上不需要再次调用 plot
,因为您在函数的开头就获得了绘图的句柄。您需要做的只是更新显示的 x 和 y 数据。
我做了一些小改动,现在可以正常使用了。我重命名了侦听器回调以给它们提供更多信息的名称,并使用虚拟数据进行绘图。您将需要根据自己的数据进行调整,但这并不难。我还交换了两个滑块的初始值。
这是代码。我在 added/modified 东西的地方添加了 %// === 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
以及移动两个滑块后的屏幕截图:
为了得到XNew
我添加了一个按钮。将 Xnew = X(n2:n3)
放在函数的最后一个 end
之前会导致错误,因为启动函数时既没有定义 n2
也没有定义 n3
,因为 Matlab 从头到尾读取所有代码底部。因此按一个按钮获取值更安全。