二十一、GUI设计

21.1创建GUI

image.png
image.png
image.png
双击任一区域,可以进行相关设置;

21.2 回调函数

案例一:实现按键后数据在静态框内显示指定文本

  1. 首先我们需要先找到对应控件的命名;

image.png

  1. 通过贯穿全局的handles来调用其他内容,来完成按键时数据显示再静态文本框内。
    1. function pushbutton1_Callback(hObject, eventdata, handles)
    2. % hObject handle to pushbutton1 (see GCBO)
    3. % eventdata reserved - to be defined in a future version of MATLAB
    4. % handles structure with handles and user data (see GUIDATA)
    5. disp([hObject.String]);
    6. %handles可以理解为图像界面的所有信息.
    7. set(handles.text2,'String','训练完成')
    ```matlab %这个函数表示界面一经显示就会调用 function demo1_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 demo1 (see VARARGIN)

% Choose default command line output for demo1 handles.output = hObject;

disp([‘hello’]);

% Update handles structure guidata(hObject, handles);

% UIWAIT makes demo1 wait for user response (see UIRESUME) % uiwait(handles.figure1);

  1. **案例二:再静态文本框内显示按按键的次数;**
  2. 1. 编辑OpeningFcn
  3. handles.count = 0;定义一个全局变量<br />%把handles当前的数据保存到gcf当中;<br />guidata(gcf,handles);
  4. ```matlab
  5. function demo1_OpeningFcn(hObject, eventdata, handles, varargin)
  6. % This function has no output args, see OutputFcn.
  7. % hObject handle to figure
  8. % eventdata reserved - to be defined in a future version of MATLAB
  9. % handles structure with handles and user data (see GUIDATA)
  10. % varargin command line arguments to demo1 (see VARARGIN)
  11. % Choose default command line output for demo1
  12. handles.output = hObject;
  13. handles.count = 0;
  14. disp(['hello']);
  15. % Update handles structure
  16. guidata(hObject, handles);
  17. %gcf是图形的句柄;
  18. %把handles当前的数据保存到gcf当中;
  19. guidata(gcf,handles);
  1. 设置显示数据set(handles.text2,’String’,num2str(handles.count));

    1. 保存数据;
    1. function pushbutton1_Callback(hObject, eventdata, handles)
    2. % hObject handle to pushbutton1 (see GCBO)
    3. % eventdata reserved - to be defined in a future version of MATLAB
    4. % handles structure with handles and user data (see GUIDATA)
    5. handles.count = handles.count+1;
    6. %注意此处handles.count是过滤型变量,输出需要转换格式;
    7. %handles可以理解为图像界面的所有信息.
    8. %set(handles.text2,'String','训练完成')
    9. set(handles.text2,'String',num2str(handles.count));
    10. %注意对handles进行保存
    11. guidata(gcf,handles);

案例三;根据下拉菜单来显示图像

  1. function pushbutton1_Callback(hObject, eventdata, handles)
  2. % hObject handle to pushbutton1 (see GCBO)
  3. % eventdata reserved - to be defined in a future version of MATLAB
  4. % handles structure with handles and user data (see GUIDATA)
  5. %用str接受handles下的下拉菜单popupmenu1对应的所有字符
  6. str = handles.popupmenu1.String;
  7. %获取哪个值
  8. val = handles.popupmenu1.Value;
  9. %由于ezplot显示必须要显示函数句柄或者字符串类型
  10. %而val此时为cell类型所以要完成转换;
  11. fcn = str{val};
  12. ezplot(fcn);

image.png

案例四:

  1. function start_Callback(hObject, eventdata, handles)
  2. % hObject handle to start (see GCBO)
  3. % eventdata reserved - to be defined in a future version of MATLAB
  4. % handles structure with handles and user data (see GUIDATA)
  5. % --------------------------------------------------------------------
  6. function open_Callback(hObject, eventdata, handles)
  7. % hObject handle to open (see GCBO)
  8. % eventdata reserved - to be defined in a future version of MATLAB
  9. % handles structure with handles and user data (see GUIDATA)
  10. msgbox('你打开了')
  11. demo2;
  12. % --------------------------------------------------------------------
  13. function close_Callback(hObject, eventdata, handles)
  14. % hObject handle to close (see GCBO)
  15. % eventdata reserved - to be defined in a future version of MATLAB
  16. % handles structure with handles and user data (see GUIDATA)
  17. msgbox('你关闭了')
  18. %questdlg返回一个所选择的值,后面的属性按照顺序来选。
  19. r = questdlg('你要关闭吗?','关闭请求','是','否','否');
  20. %相对于r=='是'来做判断,strcmp更加规范一些。
  21. if strcmp(r,'是')
  22. close all;
  23. end