您的位置:首页 > 编程语言 > MATLAB

MATLAB GUI

2016-01-19 21:02 751 查看
testgui.m

function varargout = testgui(varargin)
% TESTGUI MATLAB code for testgui.fig
%      TESTGUI, by itself, creates a new TESTGUI or raises the existing
%      singleton*.
%
%      H = TESTGUI returns the handle to a new TESTGUI or the handle to
%      the existing singleton*.
%
%      TESTGUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in TESTGUI.M with the given input arguments.
%
%      TESTGUI('Property','Value',...) creates a new TESTGUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before testgui_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to testgui_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help testgui

% Last Modified by GUIDE v2.5 17-Jan-2016 22:54:45

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
'gui_Singleton',  gui_Singleton, ...
'gui_OpeningFcn', @testgui_OpeningFcn, ...
'gui_OutputFcn',  @testgui_OutputFcn, ...
'gui_LayoutFcn',  [] , ...
'gui_Callback',   []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before testgui is made visible.
function testgui_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 testgui (see VARARGIN)
handles.peaks=peaks(35);
handles.membrane=membrane;
[x,y]=meshgrid(-8:.5:8);
r=sqrt(x.^2+y.^2)+eps;
sinc=sin(r)./r;
hanles.sinc=sinc;
handles.current_data=handles.peaks;
surf(handles.current_data)
% Choose default command line output for testgui
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

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

% --- Outputs from this function are returned to the command line.
function varargout = testgui_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

% --- 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)
surf(handles.current_data);

% --- 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)
mesh(handles.current_data);

% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (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)

% --- 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)
str=get(hObject,'String');
val=get(hObject,'Value');
switch str{val};
case 'peaks'
handles.current_data=handles.peaks;
case 'membrane'
handles.current_data=handles.membrane;
case 'sinc'
handles.current_data=handles.sinc;
end
guidata(hObject,handles);
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1

% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end


(uimenu)菜单的使用:

clc
clear
close all
figure
uim=uimenu('label','Option','Position',4);
uim_1=uimenu(uim,'label','grid on','callback','grid on');
uim_2=uimenu(uim,'label'
1481a
,'grid off','callback','grid off','Separator','on');
uim_3=uimenu(uim,'label','box on','Callback','box on');
uim_4=uimenu(uim,'label','box of','Callback','box off');
uim_5=uimenu(uim,'label','Figure Color','Separator','on','Visible','on');%%可用性开关,将菜单设置为可见。
uim_5_1=uimenu(uim_5,'label','Red','callback','set(gcf,''Color'',''r'')');
uim_5_2=uimenu(uim_5,'label','Blue','ForeGroundColor','m','callback','set(gcf,''Color'',''b'')');%%设定文字的颜色为品红,背景色为蓝色。
uim_5_3=uimenu(uim_5,'label','Green','callback','set(gcf,''Color'',''g'')','Checked','on','Visible','on','Enable','off');%%使能开关,回调函数失效。
get(uim_5_3,'Position')


contexmenu

t=0:pi/100:2*pi;
y=sin(t).*cos(3*t);
h=plot(t,y);
uicm=uicontextmenu;%%uicontextmenu句柄
uimenu(uicm,'label','Red','callback','set(h,''color'',''r'')');%右键单击选择Red,执行callback
uimenu(uicm,'label','Green','callback','set(h,''color'',''g'')');
uimenu(uicm,'label','Blue','callback','set(h,''color'',''b'')');
set(h,'uicontextmenu',uicm)


Panel

clc
clear
close all
hf=figure('Position',[200,200,600,400],'Name','UIcontrol1','NumberTitle','off');
hp=uipanel('units','pixels',...
'Position',[40 80 160 100],...
'Title','Panel',...
'FontSize',12);
ha=axes('Position',[0.4 0.1 0.5 0.7],...
'Box','on');
hbg=uibuttongroup('units','pixels',...
'Position',[48 178 104 70],...
'Title','Button Group');
hsin=uicontrol(hf,...
'Style','pushbutton',...
'Position',[50 120 150 30],...
'String','Plot normal Distributon function',...
'Callback',['subplot(ha);'...
'x=-10:0.1:10;'...
'pd=makedist(''norm'',0,1);'...
'plot(x,pdf(pd,x));'...
'axis([-10,10,0,0.4]);'...
'grid on;'...
'xlabel(''x'');'...
'syms x;'...
'y=1/sqrt(2*pi*x)*exp(x^2/2);'...
'ylabel(''y'');'...

]);
hclose=uicontrol(hf,...
'Style','pushbutton',...
'Position',[50 100,150 30],...
'String','Exit',...
'Callback','close(hf)');
hboxon=uicontrol(gcf,...
'Style','radio',...
'Value',1,...
'Position',[50,220,100,10],...
'String','set box on',...
'Callback',['set(hboxon,''Value'',1);'...
'set(hboxoff,''Value'',0);'...
'set(gca,''box'',''on'')']);
hboxoff=uicontrol(gcf,...
'Style','radio',...
'Value',0,...
'Position',[50,200,100,10],...
'String',' set box off',...
'Callback',[...
'set(hboxoff,''Value'',1);'...
'set(hboxon,''Value'',0);'...
'set(gca,''box'',''off'')';
]);


slider

%绘制正态分布函数曲线其中u小时均值范围从[-5 5].标准差[1 5]
clc
clear
close all
PlotNorm=[...
'Num=get(hs,''Value'');'...
'u=get(hs2,''Value'');'...
'subplot(ha);'...
'x=-10:0.1:10;'...
'md=makedist(''Norm'',u,Num);'...
'y=pdf(md,x);'...
'plot(x,y);'...
'axis([-10 10 0 max(y)]);'...
'xlabel(''x'');'...
'ylabel(''y=NormPdf(x)'');'...
'if get(hcGrid,''Value'')==1;'...
'grid on;'...
'else;'...
'grid off;'...
'end;'...
];
hf=figure('Position',[200 200 600 400],...
'Name','Uicontrol',...
'NumberTitle','off');
ha=axes('Position',[0.4 0.1 0.5 0.7],...
'Box','on');
hnorm=uicontrol(hf,...
'Style','pushbutton',...
'Position',[50 140 100 30],...
'String','plot NormPdf(x)',...
'Callback',PlotNorm);
hbClose=uicontrol(hf,...
'Style','pushbutton',...
'Position',[50 100 100 30],...
'String','Exit',...
'Callback','close(hf)');
hboxoff=uicontrol(hf,...
'Style','radio',...
'Position',[50 180 100 20],...
'Value',0,...
'String','Set Box off',...
'CallBack',[...
'set(hboxoff,''Value'',1);'...
'set(hboxon,''Value'',0);'...
'set(gca,''Box'',''off'');']);
hboxon=uicontrol(hf,...
'Style','radio',...
'Position',[50 210 100 20],...
'String','Set Box on',...
'Value',1,...
'CallBack',[...
'set(hboxoff,''Value'',0);'...
'set(hboxon,''Value'',1);'...
'set(gca,''Box'',''on'');']);
hcGrid=uicontrol(hf,...
'Style','check',...
'Position',[50 240 100 20],...
'String','Grid on',...
'Value',1,...
'CallBack',['if get(hcGrid,''Value'')==1;'...
'grid on;'...
'else;'...
'grid off;'...
'end';]);
hceNum=uicontrol(hf,'Style','edit',...
'Position',[50 270 100 20],...
'String','4',...
'CallBack','set(hceNum,''String'',num2str(Num))');
htpi=uicontrol(hf,'Style','text',...
'Position',[150 270 20 20],...
'String','Pi');
htminmax=uicontrol(hf,'Style','text',...
'Position',[50 330 100 20],...
'String','1        5');
hs=uicontrol(hf,'Style','slider',...
'Position',[50 310 100 20],...
'Value',4,...
'Min',1,...
'Max',5,...
'CallBack',PlotNorm);
ht=uicontrol(hf,'Style','text',...
'Position',[150 310 100 20],...
'String','设置标准差');
ht2=uicontrol(hf,'Style','text',...
'Position',[150 340 100 20],...
'String','设置均值');
hs2=uicontrol(hf,'Style','slider',...
'Position',[50 340 100 20],...
'Value',0,...
'Min',-5,...
'Max',5,...
'CallBack',PlotNorm);


pupupbutton

clear
clc
close all
PlotSin=[...
'Num=get(hs,''Value'');'...
'set(heNum,''String'',num2str(Num));'...
'subplot(ha);'...
'x=0:pi/100:pi*Num;'...
'plot(x,sin(x));'...
'axis([0 Num*pi -1 1]);'...
'xlabel(''x'');'...
'ylabel(''y=sin(x)'');'...
'if get(hcGrid,''Value'')==1;'...
'grid on;'...
'else;'...
'grid off;'...
'end;'...
'UD=get(hpcolor,''UserData'');',...
'set(gcf,''Color'',UD(get(hpcolor,''Value''),:));'...
];
hf=figure('Position',[200 200 600 400],...
'Name','Uicontrol1',...
'NumberTitle','off');
ha=axes('Position',[0.4 0.1 0.5 0.7],...
'Box','on');
hbSin=uicontrol(hf,...
'Style','pushbutton',...
'Position',[50 140 100 30],...
'String','Plot Sin(x)',...
'CallBack',PlotSin);
hbClose=uicontrol(hf,'Style','pushbutton',...
'String','Exit',...
'CallBack','close(hf)');
hrboxoff=uicontrol(hf,'Style','radio',...
'Position',[50 180 100 20],...
'String','Set box off',...
'Value',0,...
'CallBack',[...
'set(hrboxon,''Value'',0);'...
'set(hrboxoff,''Value'',1);'...
'set(gca,''Box'',''off'');']);
hrboxon=uicontrol(hf,'Style','radio',...
'Position',[50 210 100 20],...
'String','Set Box on',...
'Value',1,...
'CallBack',[...
'set(hrboxon,''Value'',1);'...
'set(hrboxoff,''Value'',0);'...
'set(gca,''Box'',''on'');']);
hcGrid=uicontrol(hf,'Style','check',...
'Position',[50 240 100 20],...
'String','Grid on',...
'Value',1,...
'CallBack',[...
'if get(hcGrid,''Value'')==1;'...
'grid on;'...
'else;'...
'grid off;'...
'end';]);
heNum=uicontrol(hf,...
'Style','edit',...
'Position',[50 270 100 20],...
'String','4',...
'CallBack','set(heNum,''String'',num2str(Num))');
htpi=uicontrol(hf,'Style','text',...
'Position',[150 270 20 20],...
'String','pi');
htminmax=uicontrol(hf,'Style','text',...
'Position',[50 330 100 20],...
'String','1pi        20pi');
hs=uicontrol(hf,...
'Style','slider',...
'Position',[50 310 100 20],...
'Value',4,...
'Min',1,...
'Max',20,...
'CallBack',PlotSin);
hpcolor=uicontrol(gcf,...
'Style','popupmenu',...
'Position',[340 360 100 20],...
'String','Black|Red|Yellow|Green|Cyan|Blue|Magental|White',...
'Value',1,...
'UserData',[...
[0 0 0];...
[1 0 0];...
[1 1 0];...
[0 1 0];...
[0 1 1];...
[0 0 1];...
[1 0 1];...
[1 1 1]],...
'CallBack',PlotSin);


toogle

hf=figure('Position',[200 200 600 400],...
'Name','Uicontrol1',...
'NumberTitle','off');
ha=axes('Position',[0.4 0.1 0.5 0.7],...
'Box','on',...
'XGrid','on',...
'YGrid','on');
htg=uicontrol(gcf,'style','toggle',...
'String','Grid on',...
'position',[50 200 100 40],...
'callback',[...
'grid;'...
'if length(get(htg,''String''))==7&get(htg,''String'')==''Grid on'';'...
'set(htg,''String'',''Grid off'');'...
'else;'...
'set(htg,''String'',''Grid on'');'...
'end;'...
]);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: