当前位置:首页 » 《休闲阅读》 » 正文

Matlab GUI设计基础范例(可以一步一步跟着做)

22 人参与  2024年04月19日 11:18  分类 : 《休闲阅读》  评论

点击全文阅读


我们要做一个GUI界面,可以选择peaks、membrane和sinc三种三维图数据,选择画出surf、mesh和contour三种图像。

打开GUI

每个版本打开方式可能都不一样,但有一个是相同的,就是在命令行输入guide回车。

绘制控件

大概就绘制成这样,在左边的工具栏拖动出来就行。选中多个控件,点击上面的串串,还可以进行对齐和等间距分布。

设置控件属性

对着控件双击即可,普通按钮的string属性设置为Surf/Mesh/Contour,Tag属性设置为surf_pushbutton/mesh_pushbutton/contour_pushbutton。有兴趣可以改点大小颜色什么的。

下拉菜单的String会特殊一点,得写几行。

M文件编写

我一开始以为要自己另外新建一个M文件,然后发现其实只需要把刚才的界面保存一下就好了,界面会保存为一个.fig文件,然后自动生成一个.m文件,里面有每个控件对应的处理函数。名字很容易找,surf_pushbutton_Callback就是surf的普通按钮的回调函数,在下面写上自己想写的内容就行。

untitled_OpeningFcn是整个GUI打开时候执行的函数

% --- Executes just before untitled is made visible.function untitled_OpeningFcn(hObject, eventdata, handles, varargin)% 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 untitled (see VARARGIN)handles.peaks=peaks(35);handles.membrane=membrane;[x,y]=meshgrid(-8:0.5:8);r=sqrt(x.^2+y.^2)+eps;sinc=sin(r)./r;handles.sinc=sinc;%创建结构体,保存3组数据handles.current_data=handles.peaks;%默认绘制peaks图像surf(handles.current_data);%绘制图形% Choose default command line output for untitledhandles.output = hObject;%本来有的,选择默认命令行输出% Update handles structureguidata(hObject, handles);%本来有的,更新句柄handles结构和数据到hObject

在surf_pushbutton_Callback里面增加绘制图像命令,另外两个普通按钮也一样。

% --- Executes on button press in surf_pushbutton.function surf_pushbutton_Callback(hObject, eventdata, handles)% hObject    handle to surf_pushbutton (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)surf(handles.current_data);%使用当前数据绘制surf图像

popupmenu1_Callback是下拉菜单的回调函数

% --- 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)val=get(hObject,'Value');str=get(hObject,'String');switch str{val}%判断选择了哪一组数据,将当前内容更新为对应的数据case 'peaks'    handles.current_data = handles.peaks;case 'membrane'    handles.current_data = handles.membrane;  case 'sinc'    handles.current_data = handles.sinc;  endguidata(hObject,handles)%更新句柄handles结构和内容到hObject% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array%        contents{get(hObject,'Value')} returns selected item from popupmenu1

做到这里,项目有了个大致的模样。

窗口菜单

接下来就是锦上添花了

使用菜单编辑器新建菜单

添加代码

% --------------------------------------------------------------------function Close_menu_Callback(hObject, eventdata, handles)% hObject    handle to Close_menu (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% --------------------------------------------------------------------function CloseF_botton_Callback(hObject, eventdata, handles)% hObject    handle to CloseF_botton (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)close(gcf);% --------------------------------------------------------------------function Surf_button_Callback(hObject, eventdata, handles)% hObject    handle to Surf_button (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)surf(handles.current_data);% --------------------------------------------------------------------function Mesh_button_Callback(hObject, eventdata, handles)% hObject    handle to Mesh_button (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)mesh(handles.current_data);% --------------------------------------------------------------------function Contour_botton_Callback(hObject, eventdata, handles)% hObject    handle to Contour_botton (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)contour(handles.current_data);

实验效果

上下文菜单

这个名字不太好理解,就是对着对象右击就可以出来的菜单。

这里有个小技巧,当m文件太大的时候,点击查看就可以跳转到m文件对应的回调函数。

选择我们的上下文菜单

编写程序

% --------------------------------------------------------------------function box_menu_Callback(hObject, eventdata, handles)% hObject    handle to box_menu (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)if strcmp(get(gcbo,'checked'),'on')    set(gcbo,'checked','off');else    set(gcbo,'checked','on');   endbox;% --------------------------------------------------------------------function grid_menu_Callback(hObject, eventdata, handles)% hObject    handle to grid_menu (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)if strcmp(get(gcbo,'checked'),'on')    set(gcbo,'checked','off');else    set(gcbo,'checked','on');   endgrid;

实验效果

对象浏览器

可以看到设计里面的所有对象,双击就能进属性。

GUI保存

会保存两个文件,fig和m。

fig是二进制文件,保存元件布局

m文件保存每个回调函数和具体执行


点击全文阅读


本文链接:http://zhangshiyu.com/post/97476.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1