MATLAB GUI界面中的axes显示m文件绘制的图。或者说我已经在m文件绘制好了一个双xy轴的图,但是我把它放到gui界面用按钮调用,axes只显示一个xy轴;所以我想直接把m文件绘制的图在axes中显示,该如何操作。
在MATLAB GUI界面中显示已经绘制好的双xy轴的图像,可以按照以下步骤进行操作:
在GUI界面中添加一个axes控件,使用Tag值来标识该控件,例如设为axes1。
在绘制好双xy轴的图像的m文件中,将图像绘制在axes1中,代码示例:
x = 0:0.1:10;
y1 = sin(x);
y2 = cos(x);
axes(handles.axes1);
plot(x,y1);
hold on;
plot(x,y2);
xlabel('x轴');
ylabel('y轴');
legend('sin(x)', 'cos(x)');
axes(handles.axes1); % 设置当前绘图区域为axes1
cla; % 清空绘图区域
run('your_m_file.m'); % 运行绘制图像的m文件
其中,your_m_file.m表示绘制双xy轴图像的m文件名。
完整的代码示例:
GUI界面回调函数中的代码:
function show_plot_button_Callback(hObject, eventdata, handles)
% hObject handle to show_plot_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes1); % 设置当前绘图区域为axes1
cla; % 清空绘图区域
run('your_m_file.m'); % 运行绘制图像的m文件
your_m_file.m中的代码:
x = 0:0.1:10;
y1 = sin(x);
y2 = cos(x);
axes(handles.axes1);
plot(x,y1);
hold on;
plot(x,y2);
xlabel('x轴');
ylabel('y轴');
legend('sin(x)', 'cos(x)');
其中,show_plot_button为GUI界面中的按钮标签,handles.axes1为GUI界面中的axes控件的Tag值。
仍然没有显示。