matlab gui 清除图形

我用subplot函数绘制了2*1个图,现在想点击按钮就可以消去这两个图的所有东西,请问这个按钮的回调函数怎么写?

1、首先,打开matlab编程软件,如图所示,单击左上角的New并选择New GUI project。

2、然后会弹出一个对话框,选择要创建的GUI样式,在这里默认选择第一个,创建一个空白的GUI。

3、然后在新界面上放置坐标轴、三个绘图按钮和一个清除按钮。

4、输入代码并向open初始化函数添加一个handle变量, handles.x=-pi:0.01:pi。

5、绘制sin函数,在sin按钮的回调函数中编写以下代码。

6、在画出图形后,还需要完成清除图像的功能,并进入清除按钮的回调函数。

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-11-18
假设这两个subplot的句柄是handles_ax1,handles_ax2,你可以直接用cla(handles_ax1)清除图像,比如:
>> a=0:0.01:pi;
>> ax1=SUBPLOT(2,1,1);plot(a,sin(a));
>> ax2=SUBPLOT(2,1,2);plot(a,cos(a));
>> cla(ax1);cla(ax2);追问

界面上是需要点击按钮来消除的,比如点击“运行”按钮就显示图,即“运行”按钮回调函数a=0:0.01:pi;
>> ax1=SUBPLOT(2,1,1);plot(a,sin(a));
>> ax2=SUBPLOT(2,1,2);plot(a,cos(a));
之后点击“清除”就会消图,即“清除”回调函数是cla(ax1);cla(ax2);
我试了下不行

追答

pushbotton1是画图的,pushbutton2是清除的,你要做一个数据传递的工作,因为callback中的变量都是局部变量:
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
a=0:0.01:pi;
ax1=SUBPLOT(2,1,1);plot(a,sin(a));
ax2=SUBPLOT(2,1,2);plot(a,cos(a));
setappdata(handles.pushbutton1,'ax1',ax1)
setappdata(handles.pushbutton1,'ax2',ax2)

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
ax1=getappdata(handles.pushbutton1,'ax1');
ax2=getappdata(handles.pushbutton1,'ax2');
cla(ax1)
cla(ax2)

本回答被提问者采纳
相似回答