matlab GUI(one)

1. 代开GUI界面

  命令行输入guide:

 2.1 创建按钮

  点击“ok”按钮,在视图上创建一个按钮:

 2.2 查看按钮信息

  双击按钮:

 

   可以通过style来调节不同的样式。

3. 回调函数

  在pushbutton上面右键到查看回调,点击Callback,便可进入回调函数的定义界面。

4. 简单的文本框传递数据

   首先,创建一个 文本框,双击图标,修改字体大小,同时string属性改为空白显示。

   编写pushbutton的回调函数:

   之后我们运行:

 5. 显示当前时间

  我们采用定时器,每1秒刷新一次,即触发一次函数。

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)

% Choose default command line output for untitled
handles.output = hObject;
h=timer;   %定时器
handles.he=h;   %将定时器放到全局变量中
%set(handles.he,'ExecutionMode','singleShot');  %定时器只执行一次,定一次时。
set(handles.he,'ExecutionMode','fixedRate');   %定时器,循环执行,循环定时。
set(handles.he,'Period',1);    %定时器,定时间隔 1秒
set(handles.he,'TimerFcn',{@disptime,handles});    %定时器,定时会触发 TimerFcn 函数,定时函数(TimerFcn)触发用户自定义的函数(disptime函数)
start(handles.he);   %开启定时器
% Update handles structure
guidata(hObject, handles);

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

% 自定义的函数,将edit控件的内容改成当前时间。定时器,定时会触发该函数
function disptime(hObject, eventdata, handles)
set(handles.edit1,'String',datestr(now));   % 将edit控件的内容改成当前时间

 6. 按钮的使用方法

  单选框,复选框和切换按钮是一样的,都有Max,min属性。在改变max和min的值的时候,击得要修改value属性为其中一个。

 仅展示两个回调,代码格式完全一样。

function rb1_Callback(hObject, eventdata, handles)
% hObject    handle to rb1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of rb1
var=get(handles.rb1,'value');
set(handles.rb2,'string',num2str(var));




function rb3_Callback(hObject, eventdata, handles)
% hObject    handle to rb3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of rb3
var=get(handles.rb3,'value');
set(handles.rb4,'string',num2str(var));

 7. 按钮组的使用方法

  首先,同样的在figure中绘制控件。

  其次,可以调整按钮组和坐标系的位置,鼠标左击 导航栏“对其对象”:

   其次,选中按钮组,右键 查看回调,SelectChangeFun

   完成编写:

function uibuttongroup1_SelectionChangedFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in uibuttongroup1 
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x=0:0.01:2*pi;
current_oab=get(eventdata.NewValue,'Tag');
axes(handles.axes1)
switch current_oab
    case 'ag1'
        y=sin(x);
        plot(x,y)
    case 'ag2'
        y=cos(x);
        plot(x,y)
    case 'ag3'
        y=cos(x)+sin(x);
        plot(x,y)
end

 

posted @ 2021-04-08 17:44  为红颜  阅读(327)  评论(0编辑  收藏  举报