GUI设计小程序代码_第1页
GUI设计小程序代码_第2页
GUI设计小程序代码_第3页
GUI设计小程序代码_第4页
GUI设计小程序代码_第5页
已阅读5页,还剩30页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、matlab GUI设计  能产生正弦信号、方波、三角波源代码如下:function varargout = Mytools(varargin)gui_Singleton = 1;gui_State = struct('gui_Name',       mfilename, .                   'gui_

2、Singleton',  gui_Singleton, .                   'gui_OpeningFcn', Mytools_OpeningFcn, .                 

3、0; 'gui_OutputFcn',  Mytools_OutputFcn, .                   'gui_LayoutFcn',  , .                 &

4、#160; 'gui_Callback',   );if nargin & isstr(varargin1)    gui_State.gui_Callback = str2func(varargin1);endif nargout    varargout1:nargout = gui_mainfcn(gui_State, varargin:);else    gui_mainfcn(gui_State, varargin:);endfunction Myto

5、ols_OpeningFcn(hObject, eventdata, handles, varargin)handles.output = hObject;guidata(hObject, handles);function varargout = Mytools_OutputFcn(hObject, eventdata, handles)varargout1 = handles.output;function pushbutton1_Callback(hObject, eventdata, handles)t=0:0.001:6;y=sin(0.5*pi*t);plot(t,y);title

6、('正弦信号');grid on;axis(-0.5 6.2 -1.2 1.2);set(handles.text1,'String',' 说明:周期为4,幅值为2的正弦波信号');function edit2_CreateFcn(hObject, eventdata, handles)if ispc    set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundC

7、olor',get(0,'defaultUicontrolBackgroundColor');endfunction pushbutton8_Callback(hObject, eventdata, handles)function pushbutton10_Callback(hObject, eventdata, handles)t=0:0.001:3;y=square(2*pi*t,50);plot(t,y);grid on;axis(0,3,-1.2,1.2);title('方波信号');set(handles.text1,'String&

8、#39;,' 说明:周期为1,幅值为2的方波信号');function pushbutton9_Callback(hObject, eventdata, handles)t=-6:0.01:6;ft=sawtooth(pi*t,0.5);plot(t,ft);grid on;title('三角波信号');axsi(-6 6 -1.2 1.2);set(handles.text1,'String',' 说明:周期为2,幅值为2的三角波信号');MATLAB实例默认分类 2009-06-22 22:06:47 阅读998 评论0 &#

9、160; 字号:大中小 订阅         这个程序主要是一个递推的最小二乘发,根据diphon方程来辨识一类系统的参数.        在matlab的comand窗口,输入guide,就会弹出控件设计的工具,有button,text edit.可以通过属性检查器来改变一些属性如果用过VB的人应该知道怎么回事,点击右键就可以看到了.主要是设计回调函数,不同句柄的数据传送,比较麻烦,而且发现BUG比较多.直接用命令的形式编程也可以,编

10、写简单的GUI到是很方便的.   在gui界面里可以输入遗忘因子,迭代次数,输入函数.源程序function varargout = lsmgui(varargin)% LSMGUI M-file for lsmgui.fig%      LSMGUI, by itself, creates a new LSMGUI or raises the existing%      singleton*.%      H = LSMG

11、UI returns the handle to a new LSMGUI or the handle to%      the existing singleton*.%      LSMGUI('CALLBACK',hObject,eventData,handles,.) calls the local%      function named CALLBACK in LSMGUI.M with the given input

12、 arguments.%      LSMGUI('Property','Value',.) creates a new LSMGUI or raises the%      existing singleton*.  Starting from the left, property value pairs are%      applied to the GUI before lsmgui_Openin

13、gFunction gets called.  An%      unrecognized property name or invalid value makes property application%      stop.  All inputs are passed to lsmgui_OpeningFcn via varargin.%      *See GUI Options on GUIDE's Too

14、ls 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 lsmgui% Last Modified by GUIDE v2.5 30-Nov-2006 23:59:34% Begin initialization code - DO NOT EDI

15、Tgui_Singleton = 1;gui_State = struct('gui_Name',       mfilename, .                   'gui_Singleton',  gui_Singleton, .      &#

16、160;            'gui_OpeningFcn', lsmgui_OpeningFcn, .                   'gui_OutputFcn',  lsmgui_OutputFcn, .    

17、               'gui_LayoutFcn',  , .                   'gui_Callback',   );if nargin & isstr(varargin1)&#

18、160;   gui_State.gui_Callback = str2func(varargin1);endif nargout    varargout1:nargout = gui_mainfcn(gui_State, varargin:);else    gui_mainfcn(gui_State, varargin:);end% End initialization code - DO NOT EDIT% - Executes just before lsmgui is made visible.func

19、tion lsmgui_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 GUI

20、DATA)% varargin   command line arguments to lsmgui (see VARARGIN)% Choose default command line output for lsmguihandles.output = hObject;% Update handles structureguidata(hObject, handles);initialize_gui(hObject, handles);% UIWAIT makes lsmgui wait for user response (see UIRESUME)% uiwait(

21、handles.figure1);% - Outputs from this function are returned to the command line.function varargout =lsmgui_OutputFcn(hObject, eventdata, handles)% varargout  cell array for returning output args (see VARARGOUT);% hObject    handle to figure% eventdata  reserved - to be defi

22、ned in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Get default command line output from handles structurevarargout1 = handles.output;% - Executes during object creation, after setting all properties.function edit1_CreateFcn(hObject, event

23、data, handles)% hObject    handle to edit1 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.% 

24、;      See ISPC and COMPUTER.if ispc    set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor');end function edit1_Callback(hObject, eventdata, ha

25、ndles)n= str2double(get(hObject, 'String');if isnan(n)             %输入迭代次数    set(hObject, 'String', 0);    errordlg('Input must be a number','Error');enddata = getappdata(gcbf,

26、'metricdata');data.n = n;setappdata(gcbf, 'metricdata', data);% 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(hObje

27、ct,'String') returns contents of edit1 as text%        str2double(get(hObject,'String') returns contents of edit1 as a double% - Executes during object creation, after setting all properties.function edit2_CreateFcn(hObject, eventdata, handles)% hOb

28、ject    handle to edit2 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.%    

29、   See ISPC and COMPUTER.if ispc    set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor');end function edit2_Callback(hObject, eventdata, handles)a1 = str2doub

30、le(get(hObject, 'String');%输入参数data = getappdata(gcbf, 'metricdata');data.a1 = a1;setappdata(gcbf, 'metricdata', data);% hObject    handle to edit2 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    str

31、ucture with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit2 as text%        str2double(get(hObject,'String') returns contents of edit2 as a double% - Executes during object creation, after setting all prop

32、erties.function edit3_CreateFcn(hObject, eventdata, handles)% hObject    handle to edit3 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called% Hint: edit controls usu

33、ally have a white background on Windows.%       See ISPC and COMPUTER.if ispc    set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor');end 

34、function edit3_Callback(hObject, eventdata, handles)a2 = str2double(get(hObject, 'String');%输入参数data = getappdata(gcbf, 'metricdata');data.a2 = a2;setappdata(gcbf, 'metricdata', data);% hObject    handle to edit3 (see GCBO)% eventdata  reserved - to be def

35、ined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit3 as text%        str2double(get(hObject,'String') returns contents of edit3 as a

36、double% - Executes during object creation, after setting all properties.function edit4_CreateFcn(hObject, eventdata, handles)% hObject    handle to edit4 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not c

37、reated until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.%       See ISPC and COMPUTER.if ispc    set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundColo

38、r',get(0,'defaultUicontrolBackgroundColor');end function edit4_Callback(hObject, eventdata, handles)b1= str2double(get(hObject,'String');%输入参数data = getappdata(gcbf, 'metricdata');data.b1 = b1;setappdata(gcbf, 'metricdata', data);% hObject    h

39、andle to edit4 (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 edit4 as text%        str2doubl

40、e(get(hObject,'String') returns contents of edit4 as a double% - Executes during object creation, after setting all properties.function edit5_CreateFcn(hObject, eventdata, handles)% hObject    handle to edit5 (see GCBO)% eventdata  reserved - to be defined in a future ver

41、sion of MATLAB% handles    empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.%       See ISPC and COMPUTER.if ispc    set(hObject,'BackgroundColor','whi

42、te');else    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor');end function edit5_Callback(hObject, eventdata, handles)b2 = str2double(get(hObject, 'String');%输入参数data = getappdata(gcbf, 'metricdata');data.b2 = b2;setappdat

43、a(gcbf, 'metricdata', data);% hObject    handle to edit5 (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 e

44、dit5 as text%        str2double(get(hObject,'String') returns contents of edit5 as a double% - Executes during object creation, after setting all properties.function edit6_CreateFcn(hObject, eventdata, handles)% hObject    handle to edit6 (se

45、e GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.%       See ISPC and COMPUTER.if ispc

46、    set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor');end function edit6_Callback(hObject, eventdata, handles)i = str2double(get(hObject, 'String');if (i=0)&

47、#160;   set(hObject, 'String', 0);    errordlg('Input must not be   0','Error');    else     data = getappdata(gcbf, 'metricdata');    data.i = i;    setappdata(gcbf, 'm

48、etricdata', data);end% hObject    handle to edit6 (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 edit6 as tex

49、t%        str2double(get(hObject,'String') returns contents of edit6 as a double% - Executes during object creation, after setting all properties.function edit7_CreateFcn(hObject, eventdata, handles)% hObject    handle to edit7 (see GCBO)% ev

50、entdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.%       See ISPC and COMPUTER.if ispc  

51、;  set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor');end function edit7_Callback(hObject, eventdata, handles)f = str2double(get(hObject, 'String');if (f=1)|(f=2)|(f=3)

52、   data = getappdata(gcbf, 'metricdata');   data.f = f;   setappdata(gcbf, 'metricdata', data);else   set(hObject, 'String', 0);  errordlg('Input must be 1 or 2 or 3','Error'); end% hObject    ha

53、ndle to edit7 (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 edit7 as text%        str2double

54、(get(hObject,'String') returns contents of edit7 as a double% - Executes on button press in pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles)    data = getappdata(gcbf, 'metricdata');R=data.b1;data.b2 ;-1*data.a1 ;-1*data.a2;%已知参数k=0;%迭代次数pi=3.142;

55、W=0 0 0 0'%计算期望输出用u1 u2 y3 y4P=1000 0 0 0;0 1000 0 0;0 0 1000 0;0 0 0 1000;%4*4的迭代矩阵初值取103105T=0 0 0 0'%要辨识的参数Z=0 0 0 0'%观测的输入输出for k=1:data.n;%迭代次数Z=W;    Y(k)=R'*W;%K+1时刻的观测值,系统输出Q(k)=T'*Z;%估计的输出E(k)=Y(k)-Q(k);%辨识误差L=P*Z/(data.i+Z'*P*Z);%K(K+1)T=T+L*E(k);%一次参

56、数估计if data.i=0    errordlg('Input must be 1 or 2 or 3 ,default 1','Error');    data = getappdata(gcbf, 'metricdata');    data.i = 0.9;    setappdata(gcbf, 'metricdata', data);endP=1/data.i*(P-L*Z'*P);%P矩阵

57、迭代一次a(k)=T(1);b(k)=T(2);c(k)=T(3);d(k)=T(4);%a ,b,c,d记录估计参数的变化,绘图用if data.f = 2            %选择输入函数   u=5*sin(0.01*pi*k);%输入函数,进行辨识用   elseif data.f = 1    u=3*sin(0.01*pi*k)*3*cos(0.5*pi*k);elseif data.f=3 

58、;  u=5*sin(0.01*pi*k)*0.2*5*sin(0.1*pi*k) ;else     errordlg('Input must be 1 or 2 or 3 ,default 1','Error');    data = getappdata(gcbf, 'metricdata');    data.f = 1;    setappdata(gcbf, 'metricdata', da

59、ta);    u=3*sin(0.01*pi*k)*3*cos(0.5*pi*k); end    W(2)=W(1);W(1)=u;W(4)=W(3);W(3)=Y(k);%W=u W(1) y W(3) Z(K+1)endk=1:data.n;%可以用axis(1,100,-1.2,1)来设定坐标范围figure;subplot(1,2,1),plot(k,a(k),'r'),xlabel('迭代次数 n'),ylabel('估计参数'),title('参数的辨识'

60、),hold on;plot(k,b(k),'y'),plot(k,-c(k),'g'),plot(k,-d(k),text(1,-0.3,'绿色表示a1的估计值'),text(1,0,'蓝色辨识a2的估计值'),text(1,0.3,'红色表示b1的估计值'),text(1,0.6,'黄色表示b2的估计值'),hold offsubplot(1,2,2),plot(k,Y(k),xlabel('迭代次数 n'),ylabel('系统输出,输出估计及误差'),titl

61、e('输出信息'),hold on;plot(k,Q(k),'y'),plot(k,E(k),'r'),text(1,0.3,'蓝色表示系统输出'),text(1,0,'黄色表示估计的输出'),text(1,-0.3,'红色表示误差'),hold off% hObject    handle to pushbutton1 (see GCBO)% eventdata  reserved - to be defined in a future version of

62、 MATLAB% handles    structure with handles and user data (see GUIDATA)% - Executes on button press in pushbutton2.function pushbutton2_Callback(hObject, eventdata, handles)initialize_gui(gcbf, handles);% hObject    handle to pushbutton2 (see GCBO)% eventdata  reser

63、ved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)function initialize_gui(fig_handle, handles)data.n=10; data.a1=-1;data.a2=0.632;data.b1=0.368;data.b2=0.264;data.i=0.9; data.f = 1; setappdata(fig_handle, 'met

64、ricdata', data);set(handles.edit1, 'String',data.n);set(handles.edit2, 'String', data.a1);set(handles.edit3, 'String', data.a2);set(handles.edit4, 'string', data.b1);set(handles.edit5, 'string', data.b2);set(handles.edit6, 'String',data.i);set(hand

65、les.edit7, 'String', data.f);        标签: 专业学习   . 收藏到我的网摘 引用通告 推荐 评论 举报 打印 本文章被推荐到了0个圈子点击数: 2645  评论数: 230我 顶 !觉得精彩就顶一下,顶的多了,文章将出现在更重要的位置上。 下一篇: 我喜欢的刀刀上一篇: 辩证法与放屁博客链  和讯相册用户调查,欢迎大家参加22日预测:资金涌入 银行地产联袂领涨订阅财经手机报,参与调查赢大奖 更多.每日18时专家在线解盘超短线操作技

66、巧培训迎牛年和讯送三重大礼楚河汉界春节大放送!独家揭秘主力资金动向十大牛股引爆市场狂潮最新读者留下足迹请登录登录注册wawe1ni767匿名者(未注册) Re: matlab的GUI编程 4/9/2008 10:19:43 PM一个命令按钮,就是把这六个文本框输入的数字组成的矩阵相乘 ,请问怎么用matlab UGI 里编程,我qq282121142,请加我,谢谢引用 支持 反对 删除 不允许匿名评论  匿名者(未注册) Re: matlab的GUI编程 5/11/2007 5:20:21 PM你好请问文件中的保存、退出的代码怎么编写?谢谢!急!引用 支持 反对 删除 不允许匿名评论

67、  zqcumt发送私信 Re: matlab的GUI编程 4/25/2007 10:39:28 PM以下是引用 小5 急求! 于 2007-4-14 16:39:05 的发言:您好!我现在正在做gui的图形界面设计的毕业设计,对里面的回调函数不是很清楚,不知道怎么编。还有很多问题想请教你,希望你多指点指点哈,能不能给我一份电子版的资料,谢了!可以的话请加我QQ372841578想和你进一步学习哈。事情有点急哈请早点联系我好吗?我的油箱:limoshiwo163 关于回调函数我在博客里转贴了别人的一些文章,可以看看应该能够很好的理解引用 支持 反对 删除 加入黑名单  zq

68、cumt发送私信 Re: matlab的GUI编程 4/25/2007 10:35:32 PM最近比较忙,所以也没时间来博客上看,对于朋友们的问题不能及时回复我推荐一本书吧。好像叫精通matlab的gui编程。网上可以下载到。我现在没时间帮你编写或者调试 程序,真是不好意思引用 支持 反对 删除 加入黑名单  匿名者(未注册) Re: matlab的GUI编程 4/23/2007 11:45:59 AMif strcmp(get(handles.run_wave,'String'),'RUN/PLOT')set(handles.run_wave,

69、9;String','STOP');cla;set(handles.display_plot,'HandleVisibility','On')set(handles.time_plot,'HandleVisibility','Off')cla; % clear figureset(gca,'UserData',1)pause(0.01)options = odeset('RelTol',1e-4,'AbsTol',1e-4 1e-4 1e-4);t1,Y =

70、ode45(lorenzequation,t0 tfinal,y0,options,b,r,sigma);t2,DY = ode45(lorenzequation,t0 tfinal,dy,options,b,r,sigma);ball_plot = line('XData',y(1),'YData',y(2),'ZData',y(3), .'Color','Blue','Marker','.','Markersize',25,'Erase','

71、;Xor');line_plot = line('XData','YData','YData','Color','Blue','LineStyle','-','Erase','None');ball_plotd = line('XData',dy(1),'YData',dy(2),'ZData',dy(3), .'Color','Red','Marker&#

72、39;,'.','Markersize',25,'Erase','Xor');line_plotd = line('XData','YData','YData','Color','Red','LineStyle','-','Erase','None');set(handles.display_plot,'Drawmode','Fast','Visible','On','NextPlot','Add')set(handles.display_plot,'HandleVisibility','Off')set(handles.time_plot,'HandleVis

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论