【问题标题】:Matlab GUI, how to go back from subplot to a single plot?Matlab GUI,如何从子图返回到单个图?
【发布时间】:2016-06-16 13:20:42
【问题描述】:

我有一个带有两个按钮的简单 GUI。其中一个是绘制一个图,一个是绘制两个子图。但是,一旦我按下 subplot 选项,我就无法回到单个情节。我收到一个错误:

使用坐标区时出错,对象句柄无效

请看下面我的非常简单的例子:

function plot_push1_Callback(hObject, eventdata, handles)
load('test.mat')
axes(handles.axes1)
cla(handles.axes1,'reset')
plot(x,x.^(n+1));

function push_plot2_Callback(hObject, eventdata, handles)
load('test.mat')
axes(handles.axes1)
cla(handles.axes1,'reset')
subplot(2,1,1);
plot(x,x.^(0));
subplot(2,1,2);
plot(x,x);

【问题讨论】:

  • 我之前没有设计过图形用户界面,但subplot(1,1,1) 可以工作吗?
  • 我试过了,没用:(
  • 你可以试试clf
  • 我也试过clf,它正在清除整个界面。

标签: matlab matlab-figure matlab-guide subplot


【解决方案1】:

这里的主要问题是subplot 创建了一个新的axes 对象(或转换当前轴)。在操作 axes 对象时,您需要考虑到这一点。

axes(handles.axes1);    

subplot(2,1,1);             % This is still handles.axes1
plot(x, x.^(0))

newax = subplot(2,1,2);     % This is a new axes
plot(x, x);

如果你想在 GUIDE 中使用容器,我会定义一个 uipanel 而不是 axes。然后所有子图都可以存在于这个面板中。

function plot_push1_callback(hObject, eventdata, handles)
    % Make one plot in the panel
    subplot(1,1,1, 'Parent', handles.panel);

    plot(x, x.^(n+1));

function plot_push2_callback(hObject, eventdata, handles)

    % Make the first subplot in the panel
    subplot(2,1,1, 'Parent', handles.panel)

    plot(x, x.^0);

    % Make the second subplot in the panel
    subplot(2,1,2, 'Parent', handles.panel)

    plot(x, x)

【讨论】:

  • 在指南中我准备了一个专用空间(axes1),我想根据按下的按钮绘制不同的东西。我尝试使用建议的delete() 而不是我的cla(),但它也不起作用。我现在有点迷茫,你建议我应该如何安排这两个按钮?
  • @Agnieszka 子图创建了两个轴。在 GUIDE 中,您可能希望使用 uipanel 作为容器来绘制非轴中的内容。
  • @Agnieszka 添加了如何做到这一点的示例
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-23
  • 2022-08-19
  • 2011-04-14
  • 2013-03-06
  • 2011-09-27
  • 2023-03-18
  • 1970-01-01
相关资源
最近更新 更多