本文介绍matlab中简单的gui绘图界面设计
设计内容:
1、设计GUI界面:
2、通过下拉菜单选择绘制正弦、正切函数;
3、通过编辑框输入命令,根据命令绘制图形;
4、通过按钮在图形中输出标注(标题、各轴名称);
设计步骤:
1、GUI界面的设计在matlab的书籍中有介绍,这里不过多的赘述,在这里只显示响应函数的编写。
% --- Executes just before aaa is made visible. function aaa_OpeningFcn(hObject, eventdata, handles, varargin) %打开GUI设计界面后的显示 % This function has no output args, see OutputFcn. % hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to aaa (see VARARGIN) handles.membrane=membrane; %建立membrane结构体 [x,y]=meshgrid(-8:5:8); %网格采样点函数r=sqrt(x.^2+y.^2); %计算半径sina=sin(r); handles.sina=sina;cosa=cos(r); handles.cosa=cosa;tana=tan(r); handles.tana=tana; %设置sina,cosa,tana结构体 handles.current_data=handles.membrane; surf(handles.current_data) %默认打开时,输出membrane函数的三维曲线图 title('三维曲线图'); xlabel('x轴'); ylabel('y轴'); zlabel('z轴'); % Choose default command line output for aaahandles.output = hObject; % Update handles structureguidata(hObject, handles); % UIWAIT makes aaa wait for user response (see UIRESUME) % uiwait(handles.figure1);
这里aaa是我的m文件名
2、部分程序
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
var=get(handles.popupmenu1,'value');
x=0:0.01:2*pi;axes(handles.axes1)
switch var
case 1 z=sin(x); plot(x,z) handles.current_data=handles.sina; guidata(hObject, handles); title('sin(x)'); xlabel('x轴'); ylabel('y轴');
case 2 z=tan(x); plot(x,z) handles.current_data=handles.tana; guidata(hObject, handles); title('tan(x)'); xlabel('x轴'); ylabel('y轴'); end
popupmenu1是下拉菜单属性
3、部分程序
这是文本框的程序
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double var=get(handles.popupmenu1,'value');
x=0:0.01:2*pi;
switch var
case 1 y=sin(x); plot(x,y) handles.current_data=handles.sina; guidata(hObject, handles); case 2 y=tan(x); plot(x,y) handles.current_data=handles.tana; guidata(hObject, handles);
endset(gca,'XTick',0:pi/2:2*pi);set(gca,'XTickLabel',['0','pi/2','pi','3*pi/2','2*pi']);
这是显示按钮程序
function pushbutton3_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)
str=get(handles.edit1,'string');
switch str
case 'surf' surf(handles.current_data); title('');
case 'mesh' mesh(handles.current_data); title('');
case 'contour' contour(handles.current_data); title('');
end
4、程序
% --- Executes on button press in pushbutton1.
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)
var=get(handles.popupmenu1,'value');
switch var case 1
%title('sin(x)');
str=get(handles.edit1,'string');
switch str
case 'surf' title({'sin(x)';'三维曲面图'});
case 'mesh' title({'sin(x)';'三维网格图'}');
case 'contour' title({'sin(x)';'短阵等高图'}); end
case 2 %title('tan(x)');
str=get(handles.edit1,'string');
switch str
case 'surf' title({'tan(x)';'三维曲面图'});
case 'mesh' title({'tan(x)';'三维网格图'});
case 'contour' title({'tan(x)';'短阵等高图'}); end
end
(取消标注按钮)
% --- 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)title('');